Quantcast
Channel: Applied Electronics Journal
Viewing all articles
Browse latest Browse all 21

OOP Design Patterns Summary

$
0
0

Summary of OOPs Design Patterns from GoF Book

Note: Syntax, keywords of C++ used in example snippets.

Download: DesignPatterns_Summary

Creational Patterns

Singleton

Object is created only once using a static Create() method; new is not called directly.

First call of Create() method creates a static object using new.

Subsequent calls to the Create() method returns the pointer to static object already created.

Factory Method / Virtual Constructor

Objects are created using Create method instead of calling new directly.

BaseClass: abc

DerivedClasses: A,B,C

Factory:

abc CreateChar(char id) {

switch(id) {

case ‘a’: return new A();

case ‘b’: return new B();

case ‘c’: return new C();

}

}

Abstract Factory

BaseClass has CreateChar, CreateNum virtual functions

Factory1:

abc CreateChar() {

return A;

}

num CreateNum() {

return N1;

}

Factory2:

abc CreateChar() {

return B;

}

num CreateNum() {

return N2;

}

Builder

It takes many steps to create a concrete object.

Eg. Creating a tree, linked list, etc.

Prototype

Clone() method is used to copy self

Concrete objects are cloned from prototype and can be cloned again.

Inheritance: Derived class object can access base class variables, functions

Prototyping: Cloned object can access protype class variables only.

Structural Patterns

Adapter

Convert interface of a class as clients expect.

Make systems work together after they are designed.

Bridge

Similar to adapter but the interface is known in advance.

class systemA {

systemB *s;

void set(systemB *ss) { s = ss; }

void exec() { s->run(); }

}

Composite

Creating a hierarchical tree

Every node can create its own child(branch/leaf).

Each branch/leaf can be of a different type.

Decorator / Wrapper

Similar to Adapter. But interface doesn’t change. Wrapper can do additional operations.

class wrapper {

original o;

void func() {

decorate();

o.func();

}

}

Façade

Provide a higher level interface to combine many complex interfaces.

Eg. Compile() =  Scan() + Parse() + ByteCode() + Interpret()

Proxy / Surrogate

Smaller object acts on behalf of a larger object.

Eg. Document with an image. Proxy object can have only path to image file, pointer to ImageDisplay object. ImageDisplay object need not created unless Document.display() is called.

Flyweight

Use sharing to improve efficiency.

Example:

Not-Efficient:

class character {

char c;

int row, col;

}

Instead of storing a character object for every position,

Store position of every character in the alphabet.

Efficient:

class position {  int row, col; }

class character {

char c;

position p_array[];

}

 

Behavioral Patterns

 

Chain of Responsibility

Request is passed along a chain until an object finally handles it.

Split complex tasks into simpler tasks and make the system modular.

Eg. BuildHouse() calls BuildRooms();

BuildRooms() calls BuildFloors(), BuildWalls();

BuildWalls() calls BuildDoors(), BuildWindows().

Command

Encapsulate a request as an object. Each request type can have a different command object.

Iterator / Cursor

Provide a way to access elements without exposing underlying implementation.

Example:

int array[10]; // Every element can be accessed as array[i];

Iterator implementation:

class iterator {

int array[10];

int i=0;

int get() { return array[i]; i++; }

int end() { if(i<10) return 0; else return 1; }

}

Interpreter

Create a tree and execute each node recursively starting from root.

Mediator

Defines an object that encapsulates how a set objects interact. Objects need not interact by referring the other object explicitly. In Bridge pattern, two systems know the interface of the other. In Mediator pattern each system knows its interface to Mediator object.

Memento / Token

Store internal state of an object to restore later.

Eg. Moving a cursor on screen needs the position fields to be changed without changing other fields.

Memento object can store the position using get_position() method of cursor.

Modify memento with new position. Again use the memento to set_position() of cursor.

Instead of accessing internal variables of cursor, get, set methods are used along with memento.

Observer / Publish-Subscribe

Every subject keeps a list of observers. Any change in the subject updates all the observers.

Eg. Same Data can be represented as table, bar-graph, pie-chart.

State

Equivalent of having a state object for every case in a switch statement.

A state object implements task in current state, returns name of next state.

Client initializes its state-variable, calls methods in every state, changes state variable after every state.

Strategy / Policy

Do similar task using different algorithms. The family of algorithms are encapsulated and made interchangeable.

Template-Method

Define skeleton of algorithm in an operation, Let sub-class define the algorithm without changing the algorithm’s structure.

Visitor

Create a parallel tree with a visitor on each node.

Eg. Main tree does execution operation of a compiler. Visitor objects corresponding to each node does type-checking / syntax-checking.

 

  Design Patterns Summary
Creational Pattern Constructs
Singleton Object only once
Factory Method Object by ID
Abstract Factory Group of Objects by ID
Builder Object in many steps
Prototype Object by cloning another
Structural Pattern Connects
Adapter Two existing systems with different interfaces
Bridge Two systems by defining an interface
Composite Using same interface to create a tree
Wrapper To Object with same interface but more functions
Façade Multiple objects by merging interfaces
Flyweight Objects by separating fields that vary lesser
Proxy Object with light-weight interface
Behavioral Pattern Encapsulates
Chain-of-responsibility Modular sub-tasks to perform a complex task
Command Requests to perform different tasks
Iterator Private Data & Implementation with simpler interface
Interpreter Different tasks assigned to objects of a tree
Mediator Interfaces to different systems
Memento Interfaces to change private state variables
Observer Interface to tasks of different observers within broadcaster
State State specific tasks and next-state logic
Strategy Family of inter-changeable algorithms
Template Steps in algorithms by defining a skeleton
Visitor Auxiliary tasks for visitors of every node of a tree

Filed under: Bookmarks, Cpp

Viewing all articles
Browse latest Browse all 21

Trending Articles