Abstract factory design Pattern
Please take this interface for creating families or dependant objects without specifying their concrete classes. Abstract factory design pattern
This is a continuation of the software design pattern series. You can checkout the previous posts from my-blog
Abstract factory design pattern
In Abstract factory design pattern, the family of different Products can be grouped together and be instantiated without the client specifying the concrete class.
How it is different from Simple Factory pattern?
In this pattern, just like the Products implement from the Product interface, all the types of Products will be grouped together in different concrete Factories which will be implementing a factory interface. The client will be using the Factory interface without caring what implementation it is.
Cool. So how to implement Abstract Factory design pattern?
In Abstract Factory design pattern, four main component gets involved excluding the Client.
- Product Interfaces
- Concrete Product implementations
- Factory Interface
- Concrete Factory implementations
-> ProductInterface
The interface which all the products that can be instantiated should be implementing. Example: Meat
-> Concrete Product implementation
The different Concrete classes implementing the different Product interfaces. Example: WesternStyleMeat, IndianStyleMeat
-> Factory Interface
The interface which should be implemented by all the factories which is trying to create/group a family of Products.
-> Concrete Factory implementations
The Concrete classes for each different family groups which should be implementing the factory interface.
Let’s take example related to Pizzzza itself. Why? Cuz I personally love them.
Ok, to keep it simple lets take the ingredients of Pizza. Some of the most important ones are
- Cheese
- Meat
Consider there are Western type of ingredients and as well as Indian. So it’s kinda two different family of ingredients.
Consider the Western type includes ingredients like
- WesternStyleCheese
- WesternStyleMeat
and the Indian type includes
- IndianStyleCheese
- IndianStyleMeat
So we could group them to two different factories. Check the sample code.
Design Structure
photo courtesy: link
Sample Code
We can now group all the different family of products together and keep on extending to more families too. Thanks to abstract factory design pattern for that ;)
Ps: Special Thanks to Head First Design Patterns
Thank you. Hope this helps. Looking forward to more learning and sharing. Cheers :)
Connect, share and text me about your views. We can have some discussion :)
Stay connected:
Subscribe: Subscribe to Build with Sammie feed for newsletter by Email
Comments