Wednesday, March 3, 2010

Design Patterns

Design Patterns
We will discuss all the design patterns (with examples in C++) in this blog.
Creational Patterns - All the creational patterns deal with object creation means whenever there is problem/requirements related to object creation, people will use the creational design patterns.
1. Singleton - This is a creational pattern means it deals with object creation mechanism.
The basic requirement in the Singleton pattern is 'Client/Application needs only and only one object of the class'
Now the question is how we can restrict users/application to create only one instance? What will happen if the constructor is private (or protected)? obviously we can not create object of the class with private constructor. So restriction to 'do not create any object' is very easy.
The next idea to implement the requirement is to create one public function of the class which will access the private constructor and will create only one object (we can put a logic in our function to check whether the object present - if (object is present) then return object address otherwise (i.e. no object) then create a new object).