Sunday, March 27, 2011

Creational Patterns:

Creational Patterns are the design patterns which are used for constructing the objects. Below are the different types of Creational Patterns:

1. Singleton Pattern.
2. Prototype Pattern.
3. Builder Pattern.
4. Factory Pattern.
5. Factory Method Pattern
6. Abstract Factory Pattern.
7. ObjectPool Pattern.

Singleton Pattern:

The name of the pattern represents the instance of the class should be one only instance and it should be instantiable. For example, in a system there should be only one Calendar instance. Usually singletons are used for centralized management of internal or external resources and they provide a global point of access to themselves. Below are few points to be considered.
- Should have private constructor.
- Should be self instantiable.
- Should be one instance.
- Should have a utility method which returns the same instance of the Singleton object.

It involves only one class which is responsible to instantiate itself, to make sure it creates not more than one instance at the same time it provides a global point of access to that instance. In this case the same instance can be used from everywhere, being impossible to invoke directly the constructor each time.

The implementation involves a static member in the "Singleton" class, a private constructor and a static public method that returns a reference to the static member.

class Calendar {

private Calendar calendar;

private Calendar() {


public Calendar getInstance() {
if(calendar ==null) {
calendar = new Calendar();

return calendar;

Typically whenever we access Calendar, we will get the same object.

Difference between FactoryMethod/Builder Patterns:
The Factory Method is also a Creational Pattern, but is classified as a Class Creational pattern, where the Builder Pattern is an Object Creational pattern. The difference in classification is that "Class" Creational patterns deal with the relationships between the parent class and subclasses.Inheritance plays a big role in these patterns. Object Creational patterns deal with the relationship between objects and is usually created during runtime.

The Builder pattern is the creation of objects step by step. The Factory Method pattern defines an interface and let the subclasses decide which object to instantiate.

No comments:

Post a Comment