Monday, June 3, 2013

Singleton Design Pattern

Intent:-
-Restrict the instantation of class to one object and provide global point of access to it.

Problem:-
-Application needs only one instance of an Object. additionally lazy initialization and global access is necessary.

Discussion:-
Make or Declare the class with its own instance as its  static private data member. Provide a public static member function to create all initialization code and  access to instance.
Subclassing of Singleton with "Public static member function" is not possible direct need some changes. make constructor as private. Use synchornisation to avoid multiple thread may create more than one object

Structure:-
UML diagram for Singleton
Singleton.png

c++ code:-
below code is taken from:-
#include <iostream>

using namespace std;

class Singleton
{
private:
    static Singleton *single;
    Singleton()
    {
        //private constructor
    }
public:
    static Singleton* getInstance();
    void method();
    ~Singleton()
    {
    }
};

Singleton* Singleton::single = NULL;
Singleton* Singleton::getInstance()
{
/// synchronized this part based on os
    if(!single)
    {
        single = new Singleton();
    }
///////synchronized end
      return single;
}

void Singleton::method()
{
    cout << "Method of the singleton class" << endl;
}

int main()
{
    Singleton *sc1,*sc2;
    sc1 = Singleton::getInstance();
    sc1->method();
    sc2 = Singleton::getInstance();
    sc2->method();

    return 0;


}

Singleton is not good as per
 Singletons violated something called the 'Single Responsibility Principle' which states that every class you create should do one thing and one thing only because a Singleton has two distinct responsibilities: policing the instances of itself and providing configuration information.
Singletons were bad for memory management.
source:http://geekswithblogs.net/AngelEyes/archive/2013/09/08/singleton-i-love-you-but-youre-bringing-me-down-re-uploaded.aspx

Creational Design Pattern

It deals with object creation mechanism , create object suitable to situation. 

This pattern is further divided into two parts 
1. Class creational patterns 
2. Object creational patterns.

 Class creational deals with class creation and Object creational deals with object instantiation 

In greater details, 

Object-creational patterns defer part of its object creation to another object, while Class-creational patterns defer its object creation to subclasses. i.e. class-creation patterns use inheritance effectively in the instantiation process, object-creation patterns use delegation effectively to get the job done.

Following are the creational design pattern:-
1.Singleton
2.Abstract Factory
3.Builder
4.Factory Method
5.Object Pool
6.Prototype
7. Lazy initialization 

What is Design Pattern ? What is need of it?

Design pattern are general repeatable solution to a commonly occurring problems in software design. It is a description or template for how to solve a problem that can be used in many different situation.
Object oriented design patterns typically shows the relationships and interaction between classes and objects without specifying final application classes or objects that are involved.

Need / Uses of Design Patterns:-

1.Speed up the  development process . Because patterns are the proven one .
2.Well documented.
3. easy for developer to communicate as well known and well documented.

Design patterns are classified in to 3 major parts:-

1. Creational Design Pattern.
2. Structural Design Pattern.
3. Behavioral Design Pattern.