• Albert Bennett

Design Patterns: Abstract Factory/ Factory

Updated: Jul 12, 2021

Sample Link!! as in the link to the GitHub repo that has all of the code from this sample in it.

In this post I will be discussing the Abstract Factory/ Factory design pattern. Essentially you use it when you want to instantiate objects from the same family of objects. Simple to say, easy to do... ish.

The main reason for using is to help with testing. Ideally the factory itself would be injected into the object that is going to make use of it. the objects that the factory creates would need to inherit from either an interface or an (wait for it...) an abstract class. Here is a uml diagram that goes through how this all fits together:

The main difference between the abstract factory and the regular factory is in the abstraction of objects used. One has it the other doesn't. On to the code :), after all that's what we are here for.

The first step is always to define the objects that we will be instating in our factory. This is done by implementing an interface. The concrete creation of our objects will be done in the abstracted factory.

The second step is to define our factory. This again will be abstracted away so that it can be injected in when needed.

Next on to testing so that we can prove our stuff works.

Cool it works :) and there you have it an abstract factory that can be injected and used for creating objects. Neat!

* By injection I mean that you can define a parameter in a method has an interface, and then pass on in the concrete implementation of said interface. Interfaces are great as they define a contract that the implementing object must follow.

These post endings are difficult to write... have a great day/ night :)

<< More Design Patterns

12 views0 comments

Recent Posts

See All