Factory design pattern is a creational pattern- which gives you more control on how you create objects.
The main objective is to encapsulate object creation.
Factory pattern basically comes in three flavours
A) Simple factory
There are three entities. Client- who needs to create objects. Factory- who does the actual creation. Product- the objects that are finally created.
The client has a reference to a the Factory (or this method createObject may be static as well)
The client then asks the factory to create the object it wants
B) Factory Method
It has two sets of classes- Creators and Products.These are implemented as parallel hierarchies
So we have Product A, B, C - (all implementing the same interface Product I)
And in parallel- we will have multiple creators- Creator A,B,C extending Creator Abs.
The Creator Abs defines how Product will be created
Creator A , B and C have specific knowledge about how their counterpart Products will be created
No one except Creator A should know about Product A
An abstract creationMethod is provided in the Abstract Creator
This will be implemented by the Creators A ,B,C
C) Abstract Factory Pattern
This is used to create a family of related objects rather than a single object.
We start with an abstract interface which defines the suite of objects to be created
like createValidator, createPreProcesor, createPostPorocssor.
We will then create a Factory which implements all these methods
The objects that this class will create will all be related or dependent
We can accordingly have another Factory which also implements the same interface- but this one creates another set of objects - also related to each other though
The second thing we need to do is define the family of objects
e.g. you have a POValidator and a DO Validator
POPreprocessor and a DOPreprocessor
PO Validator and DO validator belong to the same family and POPreProcessor and DOPreProcessor belong to another family
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment