Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
Business is bombing at Dragon Inc. and the CEO wants to franchise all overseas branches. The first problem that comes to his attention is how they can ensure the quality of the products. They do not want franchisees to cut costs and use low quality components. Low quality toys would definitely destroy Dragon brand’s reputation. So, the CEO wants to franchise the business, but they still want to have factories located in each location to provide components. Developers gather and start to brainstorm solutions. There are three types of components used by toys; engines, wheels and rotor blades. For example, a toy car needs an engine and four wheels, while a toy helicopter uses a rotor blade and an engine.
Let us first create an abstract factory class, the back bone of all concrete factories. Note that the abstract factory essentially consists of a group of factory methods:
Then create a location specific factory (The concrete factory for creating concrete components). Let us create one for New York:
Toy class also needs to be modified.
Engines, wheels and rotor blades need to be clarified,
prepare() function needs to be abstract
(This method will be implemented by a concrete class.
In the implementation, a child class uses its location specific
ComponentsFactory to create concrete components). Modify
Let's modify the
NyHelicopter classes to implement
Lastly factory method of concrete
ToysFactory needs to be modified.
This is where to added location specific
Let's complete the code for
Now each franchise will have their components supplied by a component factory, which is controlled by Dragon Inc. HQ.
Throughout the course, we've built a franchise framework, giving Dragon Inc. control and enough freedom for each franchise to do what they're good at.
Hopefully this simple tutorial helped you with your development.
If you like our post, please follow us on Twitter and help spread the word. We need your support to continue.
If you have questions or find our mistakes in above tutorial, do leave a comment below to let us know.