Object Oriented Design Patterns
Creational Design Patterns | Structural Design Patterns | Behavioral Design Patterns |
This design patterns is all about class instantiation. This pattern can be further divided into class-creation patterns and object-creational patterns. While class-creation patterns use inheritance effectively in the instantiation process, object-creation patterns use delegation effectively to get the job done. | This design patterns is all about Class and Object composition. Structural class-creation patterns use inheritance to compose interfaces. Structural object-patterns define ways to compose objects to obtain new functionality. | This design patterns is all about Class's objects communication. Behavioral patterns are those patterns that are most specifically concerned with communication between objects. |
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 Object Pool Avoid expensive acquisition and release of resources by recycling objects that are no longer in use A fully initialized instance to be copied or cloned A class of which only a single instance can exist | Adapter Match interfaces of different classes with different philosophies Separates an object’s interface from its implementation A tree structure of simple and composite objects Add responsibilities to objects dynamically A single class that represents an entire subsystem A fine-grained instance used for efficient sharing Private Class Data Restricts accessor/mutator access An object representing another object | Chain of responsibility A way of passing a request between a chain of objects Encapsulate a command request as an object A way to include language elements in a program Sequentially access the elements of a collection Defines simplified communication between classes Capture and restore an object's internal state Null Object Designed to act as a default value of an object A way of notifying change to a number of classes State Alter an object's behavior when its state changes Encapsulates an algorithm inside a class Defer the exact steps of an algorithm to a subclass Visitor Defines a new operation to a class without change |
Abstract Factory and Factory:
Problem : Developing a common widgets Library for an application which will run on two platforms linux and windows.
Base Library.
class Widget
{
virtual public draw() = 0;
};
class WindowsButton : public Widget
{
public:
void draw() { cout << "Windows Button"; }
};
class WindowsMenu : public Widget
{
public:
void draw() { cout << "Windows Menu"; }
};
class LinuxButton : public Widget
{
public:
void draw() { cout << "Linux Button"; }
};
class LinuxButton : public Widget
{
public:
void draw() { cout << "Linux Menu"; }
};
Implementation in client side.
class AbstractWidgetFactory
{
public:
virtual Widget *GetMenu() = 0;
virtual Widget *GetButton() = 0;
};
OS Based Factory
class WindowsWidgetFactory
{
public:
Widget *GetMenu() { return new WindowsMenu; }
Widget *GetButton() { return new WindowsButton; }
}
class LinuxWidgetFactory
{
public:
Widget *GetMenu() { return new LinuxMenu; }
Widget *GetButton() { return new LinuxButton; }
}
int main()
{
AbstractWidgetFactory WidgetFactory;
#ifdef Linux
WidgetFactory = new LinuxWidgetFactory;
#elif
WidgetFactory = new WindowsWidgetFactory;
#endif
WidgetFactory.GetButton()->draw();
WidgetFactory.GetMenu()->draw();
}
Builder Pattern:
Problem : Breaking down of complicated structures into simpler components.
Entities Involved :
- Director : Responsible for calling appropriate interfaces and creating the end product
- Builder : An abstract class representing the builders for the final product
- Concrete builders : Classes which give functionality to the abstract builder object.
- Product : The final product which use the concrete builders to produce the final product.
class AbstractBuilder
{
public:
virtual void BuildPartA() = 0;
virtual void BuildPartB() = 0;
};
class BuilderA : public AbstractBuilder
{
Product product;
public:
void AddPartA() { ; }
void AddPartB() { ; }
product *GetProduct() { return &product; }
}
class BuilderB : public AbstractBuilder
{
Product product;
public:
void AddPartA() { ; }
void AddPartB() { ; }
void show();
product *GetProduct() { return &product; }
}
class Product
{
public:
List Parts;
};
class Director
{
private AbstractBuilder *_builder;
public void Construct( AbstractBuilder *builder )
{
_builder = builder;
builder->AddPartA();
builder->AddPartB();
}
public ShowProduct()
{
_builder->GetProduct().show();
}
};
Adaptor Pattern:
Need: When the default parameters of an existing object needs to be wrapped as per the new parameter requirement. This helps especially in reusing code and algorithms.
class Shape
{
int _x, _y, _x1, _x2;
public:
virtual void Draw(int x, int y, int x1, int y1) = 0;
};
class Rectangle : public Shape
{
public:
void Draw(int x, int y, int x1, int y1)
{
_x = x; _y = y; _x1 = x1; _y1 = y1; }
};
class RectangleAdaptor : public Rectangle
{
void Draw( int x, int y, int width, int height )
{
Rectangle::Draw(x, y, x + width, y + height);
}
};
The above are the ones I have used @ various parts of my career, there are a few others like the facade which i will write in the future
Superb, very brief, simple and consise....
ReplyDelete