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.
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.
Facade pattern
the Facade pattern helps manage complexity by providing a simple interface to a complicated system, making the client’s job easier and the codebase more manageable.
Why It’s Useful
Reduces complexity for the client by hiding intricate subsystem details.
Promotes loose coupling between client and subsystems.
Improves maintainability by centralizing subsystem interactions in one place.
Makes the system easier to use and understand.
When to Use It
When you want to simplify interactions with complex subsystems.
When you want to decouple clients from subsystem classes.
When you want a single entry point to a set of functionalities.
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.