Singleton Pattern
Factory Pattern
- Singleton is a creational design pattern.
- A design pattern to provide one and only instance of an object.
- Make the constructors of the class private.
- Store the object created privately.
- Provide access to get the instance through a public method.
- Can be extended to create a pool of objects.Program
#include <iostream>
using namespace std;
// Singleton class
class MySingleton {
using namespace std;
// Singleton class
class MySingleton {
public:
static MySingleton* iInstance;
static MySingleton* iInstance;
public:
static MySingleton* GetInstance();
private:
// private constructor
MySingleton();
};
MySingleton* MySingleton::iInstance = NULL;
MySingleton::MySingleton()
{
cout << "In construtor ..." << endl;
}
static MySingleton* GetInstance();
private:
// private constructor
MySingleton();
};
MySingleton* MySingleton::iInstance = NULL;
MySingleton::MySingleton()
{
cout << "In construtor ..." << endl;
}
MySingleton* MySingleton::GetInstance()
{
if ( iInstance == NULL ) {
iInstance = new MySingleton();
}
return iInstance;
{
if ( iInstance == NULL ) {
iInstance = new MySingleton();
}
return iInstance;
}
void main()
{
MySingleton* obj;
obj = MySingleton::GetInstance();
}
OUTPUT:
In construtor ... (displayed only once)
void main()
{
MySingleton* obj;
obj = MySingleton::GetInstance();
}
OUTPUT:
In construtor ... (displayed only once)
- Factory pattern is a creational design pattern.
- Idea of the factory patterns is to localize the object creation code.
- This prevents disturbing the entire system for a new type introduction.
- Typically when a new type is introduced in the system, change is at one place only where the object is created to decide which constructor to use.
- Simplest of the factory is introduction of a static method in the base class itself, which creates the required object based on the type.
- Other variant is Abstract Factory.
- Concrete classes are isolated.
- Client need not event know which class is implementing its need.
- Static factory - Sample Program
#include <iostream>
#include <string>
using namespace std;
// Abstract Base Class
class Shape {
public:
virtual void Draw() = 0;
#include <string>
using namespace std;
// Abstract Base Class
class Shape {
public:
virtual void Draw() = 0;
// Static class to create objects
// Change is required only in this function to create a new object type
static Shape* Create(string type);
};
class Circle : public Shape {
public:
void Draw() { cout << "I am circle" << endl; }
friend class Shape;
};
class Square : public Shape {
public:
void Draw() { cout << "I am square" << endl; }
friend class Shape;
};
Shape* Shape::Create(string type) {
if ( type == "circle" ) return new Circle();
if ( type == "square" ) return new Square();
return NULL;
}
void main()
{
// Give me a circle
Shape* obj1 = Shape::Create("circle");
// Give me a square
Shape* obj2 = Shape::Create("square");
obj1->Draw();
obj2->Draw();
}
OUTPUT:
I am circle
I am square
// Change is required only in this function to create a new object type
static Shape* Create(string type);
};
class Circle : public Shape {
public:
void Draw() { cout << "I am circle" << endl; }
friend class Shape;
};
class Square : public Shape {
public:
void Draw() { cout << "I am square" << endl; }
friend class Shape;
};
Shape* Shape::Create(string type) {
if ( type == "circle" ) return new Circle();
if ( type == "square" ) return new Square();
return NULL;
}
void main()
{
// Give me a circle
Shape* obj1 = Shape::Create("circle");
// Give me a square
Shape* obj2 = Shape::Create("square");
obj1->Draw();
obj2->Draw();
}
OUTPUT:
I am circle
I am square
What is Observer Pattern?
- Observer pattern is a behavioral design pattern.
- Observer pattern is used to solve the problem of notifying multiple objects of a change to keep them in sync like the Model-View-Controller (MVC) concept.
- Useful for event management kind of scenarios.
- Two classes are involved.
- The Observable class is where the actual data change is occuring. It has information about all the interested objects that need to be notified.
- The Observer is typically an abstract class providing the interface to which concrete classes interested in the events need to be compliant to.
Sample Program
#include <iostream>
#include <set>
using namespace std;
// ---------------- Observer interface -----------------
class MyObserver {
public:
virtual void Notify() = 0;
};
// ---------------- Observable object -------------------
class MyObservable {
static MyObservable* instance;
set<MyObserver*> observers;
MyObservable() { };
public:
static MyObservable* GetInstance();
void AddObserver(MyObserver& o);
void RemoveObserver(MyObserver& o);
void NotifyObservers();
void Trigger();
};
MyObservable* MyObservable::instance = NULL;
MyObservable* MyObservable::GetInstance()
{
if ( instance == NULL ) {
instance = new MyObservable();
}
return instance;
}
void MyObservable::AddObserver(MyObserver& o)
{
observers.insert(&o);
}
void MyObservable::RemoveObserver(MyObserver& o)
{
observers.erase(&o);
}
void MyObservable::NotifyObservers()
{
set<MyObserver*>::iterator itr;
for ( itr = observers.begin();
itr != observers.end(); itr++ )
(*itr)->Notify();
}
// TEST METHOD TO TRIGGER
// IN THE REAL SCENARIO THIS IS NOT REQUIRED
void MyObservable::Trigger()
{
NotifyObservers();
}
// ------ Concrete class interested in notifications ---
class MyClass : public MyObserver {
MyObservable* observable;
public:
MyClass() {
observable = MyObservable::GetInstance();
observable->AddObserver(*this);
}
~MyClass() {
observable->RemoveObserver(*this);
}
void Notify() {
cout << "Received a change event" << endl;
}
};
void main()
{
MyObservable* observable = MyObservable::GetInstance();
MyClass* obj = new MyClass();
observable->Trigger();
}
OUTPUT:
Received a change event
Can you add more examples?
ReplyDelete