Abstract factory pattern provide concept of factory of factories.
Abstract design pattern is on of the creational design pattern that provide us the interface of creating related or independent objects without specifying the concrete classes and their specifications.
In other words, Abstract design pattern is the way of organizing, How we can create groups of things that are related to each other.
Think of it, like factory of factories.
Let walk through the practical example of car production system using abstract factory design pattern for getting better understanding.
Scenario:
we are building card of two different types , ElectricCars and PetrolCars. Each car have different types of
1) Engine
2)Tyres
Goals:
Using abstract design pattern we have to create families of Electric and Petrol car parts.
What types of classes will be use in this design pattern.
1) Abstract Products (Interface).
2) Concrete Products: These are concrete classes which implement Abstract Products.
Note: Can be multiple products.
3) Abstract Factory (Interface).
4) Concrete Factories (can be multiple factories).
5) Client Code.
Core Logic
1) Abstract Products (Interfaces)
2) Concrete Products
These are classes that implement Abstract Product Interfaces
1.Concrete Product for Electric Cars
2.Concrete Product for Petrol Cars
Now we have to create Factories that implement factory but before creating the factories, let create factory(Interface)
Note: This is the factory Interface that we use in factory design pattern which implement by concrete FactoryConcrete classes to create instances/object dynamically.
3) Abstract Factory
4) Concrete Factories
5) Client Logic
Summery
Based on the study we have done so far, we can say that each factory create consistent part.
You can easily switch from petrol to electric my changing the factory, without effecting the concrete Product class.
This approach use in the scenarios where you have to build scalable modules like e.g CMS, payment gateways.
We have centralize the object creation logic.
Main code realize in interfaces(Engine, Tyre).
You can easily build more types of car e.g Hybrid car. You don't have to write entire new logic for it.
You need to add new Concrete Factory class of name HybridCarFactory.
Than create concrete product class class HybridEngine implements Engine { ... }
Comments
Post a Comment