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
Showing posts with label Design Patterns. Show all posts
Showing posts with label Design Patterns. Show all posts
Saturday, September 20, 2008
Saturday, September 13, 2008
What is a Singleton Pattern
Singleton pattern is used to ensure there is one and only one instance of a class.
There are three parts to it
A. Ensure that we cant have more than one instance of the class
This is done by marking the constructor as private or protected
B. Provide a mechanism to access the singleton object. This is typically done by providing a static method to access the object. Something like getInstance ()method
C. Creation of the object.
This is by far the most tricky part.There are many ways to do so
1) Create the object using the static initializer or Construct the object at the time of declaration
This uses the classloader to create the object. You can run into issues in a J2EE environment where there can be multiple classloader
2) Use an inner static class. The singleton object is held in a static inner class. This will ensure that the object is not created until its really needed
3) Use double-checking mechanism. This means that we create the object in the getInsatnce() method- we check for NULL before creating the object. And the call to the creation of the object is enclosed withing synchronized block
There are three parts to it
A. Ensure that we cant have more than one instance of the class
This is done by marking the constructor as private or protected
B. Provide a mechanism to access the singleton object. This is typically done by providing a static method to access the object. Something like getInstance ()method
C. Creation of the object.
This is by far the most tricky part.There are many ways to do so
1) Create the object using the static initializer or Construct the object at the time of declaration
This uses the classloader to create the object. You can run into issues in a J2EE environment where there can be multiple classloader
2) Use an inner static class. The singleton object is held in a static inner class. This will ensure that the object is not created until its really needed
3) Use double-checking mechanism. This means that we create the object in the getInsatnce() method- we check for NULL before creating the object. And the call to the creation of the object is enclosed withing synchronized block
Labels:
Design Patterns,
Interview questions,
Technology
Subscribe to:
Posts (Atom)