Design Patterns :
Strategy Pattern : The strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
Eg: with different strategies we can pay the bill via credit card,debit card,online payment etc.. each is having different algorithm.
Singleton Pattern : Ensures a class has only one instance and provides global point of access to it.
Factory Pattern : Defines an interface for creating an object, but lets sub class decide which class to instantiate. Factory method lets a class defer instantiation to subclasses.
Abstract Factory Pattern : Provides an interface for creating family of related or dependent objects without specifying their concrete classes.
Observer Pattern : Defines one to many dependency between objects so that when one object changes all its dependants are notified and updated automatically.
Singleton Pattern Example Code:
#include <iostream>
using namespace std;
class singleton
{
private:
static singleton *sptr;
singleton(){}
singleton(singleton &s);
singleton operator =(singleton &s);
public:
static singleton* GetInstance();
};
singleton *singleton::sptr=NULL;
singleton* singleton::GetInstance()
{
if(sptr==NULL)
{
sptr= new singleton();
return sptr;
}
else
{
return sptr;
}
}
int main() {
// your code goes here
singleton *ptr1 =singleton::GetInstance();
cout<<ptr1<<endl;
singleton *ptr2 =singleton::GetInstance();
cout<<ptr2;
return 0;
}
Note : Article is in Progress.
Strategy Pattern : The strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
Eg: with different strategies we can pay the bill via credit card,debit card,online payment etc.. each is having different algorithm.
Singleton Pattern : Ensures a class has only one instance and provides global point of access to it.
Factory Pattern : Defines an interface for creating an object, but lets sub class decide which class to instantiate. Factory method lets a class defer instantiation to subclasses.
Abstract Factory Pattern : Provides an interface for creating family of related or dependent objects without specifying their concrete classes.
Observer Pattern : Defines one to many dependency between objects so that when one object changes all its dependants are notified and updated automatically.
Singleton Pattern Example Code:
#include <iostream>
using namespace std;
class singleton
{
private:
static singleton *sptr;
singleton(){}
singleton(singleton &s);
singleton operator =(singleton &s);
public:
static singleton* GetInstance();
};
singleton *singleton::sptr=NULL;
singleton* singleton::GetInstance()
{
if(sptr==NULL)
{
sptr= new singleton();
return sptr;
}
else
{
return sptr;
}
}
int main() {
// your code goes here
singleton *ptr1 =singleton::GetInstance();
cout<<ptr1<<endl;
singleton *ptr2 =singleton::GetInstance();
cout<<ptr2;
return 0;
}
Note : Article is in Progress.