What I understood from design patterns...

Design patterns are the standard (documented) solutions to the common software
design problems.

From WikiPedia:

In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern isn't a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations.

Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. Algorithms are not thought of as design patterns, since they solve computational problems rather than design problems.

Anti-patterns, also referred to as pitfalls, are classes of commonly-reinvented bad solutions to problems. They are studied, as a category, in order that they may be avoided in the future, and that instances of them may be recognized when investigating non-working systems.


There are three types of design patterns:

1. Creational Patterns: Design patters which talk about the creation of objects are classes.

2. Structural Patterns: Design patterns which talk about the the composition of classes and objects.

3. Behavioral Patterns: Design Patterns which talk about the communication between the class objects.

There are many design patterns which fall in to one of the above categories, few of them are discussed below:

Creational Patterns:
1. Abstract Factory Pattern: In Abstract factory patterns, a group of factories are encapsulated into a single class. A factory is actually a class which instantiates an object.

From Wikipedia:

Use of this pattern makes it possible to interchange concrete classes without changing the code that uses them, even at runtime. However, employment of this pattern, as with similar
design patterns, incurs the risk of unnecessary complexity and extra work in the initial writing of code.


Code example:



class GUIFactory()
{
getFactory()
{
if (getfromregistry()=="OSX")
return (new OSXFactory());
else
return (new WinFactory());
}

//virtual so that the derived classes and override
virtual void createButton()=0;
};

class WinFactory: public GUIFactory
{
public Button createButton()
{
return(new Winbutton());
}
};

class OSXFactory: public GUIFactory
{
public Button createButton()
{
return (new OSXbutton());
}
};

//button classes
class WinButton:public Button
{
void paint()
{
cout<<"default paint procedure";
}
};

class OSXButton:public Button
{
void paint()
{
cout<<"OSX Paint procedure";
}
};

//Button class declaration and definition omitted here
void main()
{
GUIFactory obj1 =
GUIFactory.getFactory(); //instantiates the WinButton or OSXButton accordingly

Button abtn = obj1.createButton(); //creates appropriate button
abtn.paint();
}


2. Singleton pattern: Singleton pattern is perhaps most famous among the design patterns. This pattern ensures that the only one instance is created by the class. If another request id made, the existing instance is returned by the class. A Singleton class will have the class contructor as private or protected so that constrctor cannot be called directly. A Singleton will have an Instance() method which actually takes care of instancing the class.

There are many ways to create a singleton, here is the one of the ways to create a singleton class:


class Singleton
{
private:
Singleton(); //constructor is private
//inner class
static class SingletonHolder()
{
private:
static Singleton instance = new Singleton();
//static variable initialised with instance of outer class

};
public:
static Singleton getInstance() //static method which can be called directly to get instance
{
return SingletonHolder.instance(); //get the static variable of inner class
}
};

Here is perhaps the simplest way to create a singleton:

class Singleton
{
private:
Singleton(); //constructor is private
static Singleton test = new Singleton();
public:
static Singleton getInstance() //static method which can be called directly to get instance
{
if(test == NULL)
return new Singleton();
else
return test;
}
};


0 comments: