factory design pattern

Factory Pattern:

Put a static method in base class which creates object . Here ManageDrink is the base class and createManager is that static function.

But unlike a constructor, the actual object it returns might be an instance of a subclass. Another advantage of a factory method is that it can return existing instances multiple times based on if-else conditions while returning an instance.

class Drinks { // aka Interface
public: virtual void someMethod() = 0;

class pepsi : public Drinks {
void someMethod() { return null; }

class coke : public Drinks {
void someMethod() { return null; }

enum ManagerPepsi{
Manager1, Manager2

class ManagerDrink{
public static Manager* createManager(ManagerPepsi type) {
Manager* result = null;
switch (type) {
case Manager1:
result = new coke();
case Manager2:
result = new pepsi();
// Do whatever error logging you want
return result;

The factory design pattern, or factory method pattern (or dynamic constructor) is a mechanism for creating objects without knowing exactly what object needs to be created or how to actually create the object. A class factory provides an interface in which sub classes can implement the necessary functionality to create specific objects.

A class factory is an object for creating other objects. As classes are added to the application they register their creation routines with the class factory which then can instantiate them upon request

Abstract Factory:

Creates objects of similar classes.Used mainly for different platforms.

Implemented using virtual functions.

class Widget { public:    virtual void draw() = 0; };

class MotifButton : public Widget { public:    void draw() { cout << “MotifButton\n”; } };

class MotifMenu : public Widget { public:    void draw() { cout << “MotifMenu\n”; } };

class WindowsButton : public Widget { public:    void draw() { cout << “WindowsButton\n”; } };

class WindowsMenu : public Widget { public:    void draw() { cout << “WindowsMenu\n”; } };

class Factory {

public:    virtual Widget* create_button() = 0;

virtual Widget* create_menu() = 0;


class MotifFactory : public Factory {


Widget* create_button() {       return new MotifButton; }

Widget* create_menu()   {       return new MotifMenu; }


class WindowsFactory : public Factory {


Widget* create_button() {       return new WindowsButton; }    Widget* create_menu()   {       return new WindowsMenu; }


Factory* factory;

void display_window_one() {

Widget* w[] = { factory->create_button(),                    factory->create_menu() };

w[0]->draw();  w[1]->draw(); } 

void display_window_two() {   

Widget* w[] = { factory->create_menu(),                    factory->create_button() };   




int main() {

#ifdef MOTIF   

factory = new MotifFactory;

#else // WINDOWS   

factory = new WindowsFactory;


Widget* w = factory->create_button();





Resolving technical problems:

Solve your technical problems instantly

We provide Remote Technical Support from Monday to Sunday, 7:00PM to 1:00 AM

Mail your problem details at writeulearn@gmail.com along with your mobile numberand we will give you a call for further details. We usually attend your problems within 60 minutes and solve it in maximum 2 days.