The Patterns are divided in to groups based on the usage, objects creation, object behavior etc.
The Part 1 deals with Creational Patterns.
Creational Patterns:
Creational patterns abstract object instantiation and make the system independant of how the objects are created. Which of the follwing patterns is prefered or used will depend on the actual use case and end system requirement.
Abstract Factory: Abstract factory declares an interface for the creation of concrete products. Somestimes the concrete product is a singleton too.This can be made flexible by for example adding arguments to the abstract factory interface for creation of a family of products.
The following code is a typical implementation of abstract factory.
class abstractProduct
{
public:
virtual void useproduct()=0;
};
class abstractfactory
{
virtual abstractProduct* CreateProduct()=0;
};
class concretProduct : public abstractProduct
{
public:
void useproduct()
{
cout <<"concretProduct" << endl;
}
};
class concreteFactory : public abstractfactory
{
public:
abstractProduct* CreateProduct()
{
return new concretProduct:
}
};
class client
{
public: void use()
{
concreteFactory p ;
abstractProduct * pr = p.CreateProduct();
pr->useproduct();
}
};
Client will use only the abstract interface of product and factory.
-------------------------------------------------------------------------------------------------
Builder: Builder separate the construction of a complex object from its representation so that the same construction process can create different representations. One should use the builder pattern when the algorithm for creating a complex object should be independant of the part which make up the object and how they are assembled.
The following code describes a builder pattern:
//house and builder interface
class House
{
public: virtual void EnterHouse()=0;
protected:
virtual void buildFoundation()=0;
virtual void buildWalls()=0;
virtual void buildRoof()=0;
virtual void buildCompoundWall()=0;
};
class HouseBuilder
{
public: virtual void buildHouse()=0;
virtual House* getHouse()=0;
};
//a concreate builder and house
class LowcostContractor;
class lowcosthouse: public House
{
public: void EnterHouse()
{
cout <<"Enter Low cost House "<<>
}
friend class LowcostContractor;
private:
void buildFoundation()
{
cout <<"buildFoundation"<< endl;
}
void buildWalls()
{
cout <<"buildWalls"<< endl;
}
void buildRoof()
{
cout <<"buildRoof"<< endl;
}
void buildCompoundWall()
{
cout <<"buildCompoundWall"<< endl;
}
};
class LowcostContractor : public HouseBuilder
{
private:
House* house;
public:
LowcostContractor()
{
house = new lowcosthouse;
}
void buildHouse()
{
house->buildFoundation();
house->buildWalls();
house->buildRoof();
house->buildCompoundWall();
}
House* getHouse()
{
return house;
}
};
//a user of the concrete contarctor for a concrete house.
class Owner
{
public: void MakeHouse()
{
HouseBuilder* con= new LowcostContractor();
con->buildHouse();
House* h = con->getHouse();
//owner can use the house now.
}
};
Owner is not aware of how the components of the house is build, it just calls the contractors build house function.Here contractor acts as the builder, where the owner only needs to find the right builder, for a perticular type of house.Please note that the construction of the contrator can be from an abstract factory.
-------------------------------------------------------------------------------------------------
Factory Method: Factory methods define an interface for creating an object, but let the subclasses to decide which object to be craeted.
The following code can be part of a header file which describes the factory method.
class Factory
{
public:
virtual void runFactory() =0;
};
Factory* MakeFactory();
A factory provider for car factory implements the concrete factory as follows:
class CarFactory: public Factory
{
public: void runFactory()
{
cout <
};
Factory* MakeFactory()
{
return new CarFactory;
}
A user of the factory can use the header file for the factory method to create instances of concrete factory.
Example: Symbian Polymorphic dlls EComs etc are typical example.
-------------------------------------------------------------------------------------------------
Prototype: Prototype design pattern specify the kinds of objects to create using a prototype instance, and create new objects by copying this prototype instance. Used in frameworks to create objects of user defined classes. Supports dynamically adding objects to a frameworkwithout knowing the actual concrete type of it, a prototype registry or manager can be used here. The abstract and concrete prototypes MUST define a clone funtion for this to work.
The following example shows a prototype design pattern
class Shape
{
public: virtual ~Shape()
{
}
virtual Shape* Clone() const = 0;
};
class Circle : public Shape
{
public: Shape* Clone() const;
Circle(const Circle& me)
{
cout<<"my copy constructor\n";
}
};
Shape* Circle::Clone() const
{
return new Circle(*this);
}
class Client
{
public: Client(Shape* origin)
{
myorigin= origin;
}
Shape* Create()
{
return myorigin->Clone();
}
private:
Shape* myorigin;
};
Many practical UI editors are designed this way, where new UI objects can be added to an editor pallete.
-------------------------------------------------------------------------------------------------
SingleTon: Singleton ensures a class has only one instance and provide a global point of access to it. The following code shows a typical implementation of a singleton class.
class Singleton
{
public:
static Singleton* Create();
protected:
Singleton()
{
}
private:
static Singleton* instance;
};
Singleton* Singleton::instance =0;
Singleton* Singleton::Create()
{
if(instance ==0)
{
instance = new Singleton;
}
return instance;
}
In symbian usually the TLS is implemented this way. The singleton can also be extended to a derived singleton class by making all implementations to register to a registry of singleton and the creation always be through it. A client needing the singleton can look up the registry and get it.
-----------------------------------------------------------------------------------------------
