Design Pattern

Design pattern is a general reusable solution to a commonly occurring problem.

The Design pattern is mainly classified in to following category.

1) Creational Patterns (deal with initializing and configuring classes and objects)
Creational patterns are ones that create objects for you, rather than having you instantiate objects directly. This gives your program more flexibility in deciding which objects need to be created for a given case.

• Abstract Factory:- Creates an instance of several families of classes
• Builder: - Separates object construction from its representation
• Factory Method:- Creates an instance of several derived classes.
• Prototype:- A fully initialized instance to be copied or cloned
• Singleton:- A class in which only a single instance can exist

Note: - The best way to remember Creational pattern is by ABFPS (Abraham Became First President of States).

2) Structural Patterns (deal with decoupling the interface and implementation of classes and objects)

• Adapter:-Match interfaces of different classes.
• Bridge:-Separates an object’s abstraction from its implementation.
• Composite:-A tree structure of simple and composite objects.
• Decorator:-Add responsibilities to objects dynamically.
• Façade:-A single class that represents an entire subsystem.
• Flyweight:-A fine-grained instance used for efficient sharing.
• Proxy:-An object representing another object.

Note : To remember structural pattern best is (ABCDFFP)

3) Behavioral Patterns (deal with dynamic interactions among societies of classes and objects)

• Mediator:-Defines simplified communication between classes.
• Memento:-Capture and restore an object's internal state.
• Interpreter:- A way to include language elements in a program.
• Iterator:-Sequentially access the elements of a collection.
• Chain of Resp: - A way of passing a request between a chain of objects.
• Command:-Encapsulate a command request as an object.
• State:-Alter an object's behavior when its state changes.
• Strategy:-Encapsulates an algorithm inside a class.
• Observer: - A way of notifying change to a number of classes.
• Template Method:-Defer the exact steps of an algorithm to a subclass.
• Visitor:-Defines a new operation to a class without change.

Note: - Just remember Music....... 2 MICS On TV (MMIICCSSOTV).



1) Singleton Pattern:A class whose number of instances that can be instantiated is limited to one is called a singleton class. Thus, at any given time only one instance can exist.


class A
{
public:
static A& Instance()
{
static A singleton;
return singleton;
}

// Other non-static member functions
private:
A() {}; // Private constructor
};

2) Factory Pattern:


* This provides a simple decision making class that returns one of several possible subclasses of an abstract base class depending on the data that are provided.
* It basically one level of abstraction.
When to Use a Factory Pattern
You should consider using a Factory pattern when a class can’t anticipate which kind of class of objects it must create.

A class uses its subclasses to specify which objects it creates.

There are two issues with the code below:-
* First we have lots of ‘new’ keyword scattered in the client. In other ways the client is loaded with lot of object creational activities which can make the client logic very complicated.

Second issue is that the client needs to be aware of all types of invoices. So if we are adding one more invoice class type called as ‘InvoiceWithFooter’ we need to reference the new class in the client and recompile the client also.


if (int InvoiceType == 1) {
	objinv = new clsInvoiceWithHeader ();
}

else if (int InVoiceType == 2) {
	onjinv = new clsInvoiceWithOutHeaders ();
}