Factory Design pattern code example in C++

Factory Design pattern is achieved in 2 ways:
1. By giving a abstract interface class or and giving object create method in concrete class derived from interface class
2. By giving object create method in Factory class
2.  Factory Design pattern: defines interface to create objects. Object creation not required directly by client instead it is created in provided interface. Advantage is that client does not require to know exact class name whose object it wants to create. Also object instantiation is delayed till factory's create() method call so its like virtual constructor as exact class name is not required during compile time.
#include<iostream>
using namespace std;
#include<string.h>
class Button
{
public:
    virtual void paintButton()=0;
};
class OsLinuxButton: public Button
{
public:
    void paintButton()
    {
    cout<<"OsLinuxButton::paintButton"<<endl;
    }   
};
class OsWindowButton: public Button
{
public:
    void paintButton()
    {
    cout<<"OsWindowButton::paintButton"<<endl;
    }   
};
class GuiFactory
{
public:
    virtual Button* createButton(char* but)//No abstract
    {
    if(strcmp(but,"window")==0)
    {
    return new OsWindowButton;
    }
    if(strcmp(but,"linux")==0)
    {
    return new OsLinuxButton;
    }
    }
};
class Factory: public GuiFactory
{
public:
    Button* createButton(char* but)
    {
    if(strcmp(but,"window")==0)
    {
    return new OsWindowButton;
    }
    if(strcmp(but,"linux")==0)
    {
    return new OsLinuxButton;
    }
    }
};

int main()
{
Button *button1;
GuiFactory * guifactory=new Factory;
button1 = guifactory->createButton("window");
button1->paintButton();

Button *button2;
GuiFactory * guifactory2=new Factory;
button2 = guifactory->createButton("linux");
button2->paintButton();
return 0;
}

Example 2:
//Abstract factory Design pattern: defines interface to create family of objects. Object creation not required directly by client instead it is created in provided interface. Advantage is that client does not require to know exact class name whose object it wants to create. Also objectinstantiation is delayed till factory's create() method call so its like virtual constructor as exact class name is not required during compile time.
#include<iostream>
using namespace std;
#include<string.h>
class Button
{
public:
    virtual void paintButton()=0;
};
class OsLinuxButton: public Button
{
public:
    void paintButton()
    {
    cout<<"OsLinuxButton::paintButton"<<endl;
    }  
};
class OsWindowButton: public Button
{
public:
    void paintButton()
    {
    cout<<"OsWindowButton::paintButton"<<endl;
    }  
};
/*
class GuiFactory
{
public:
    virtual Button* createButton(char* but)=0;
};
class Factory: public GuiFactory
{
public:
    Button* createButton(char* but)
    {
    if(strcmp(but,"window")==0)
    {
    return new OsWindowButton;
    }
    if(strcmp(but,"linux")==0)
    {
    return new OsLinuxButton;
    }
    }
};
*/
int main()
{
/*
Button *button1;
GuiFactory * guifactory=new Factory;
button1 = guifactory->createButton("window");
button1->paintButton();

Button *button2;
GuiFactory * guifactory2=new Factory;
button2 = guifactory->createButton("linux");
button2->paintButton();
*/
//Without factory Object is ctreated in client code itself.
//client does new so may effect the code. Separate interface is not defined to create object.
Button *button=new OsLinuxButton;
button->paintButton();

Button *buttonWindow=new OsWindowButton;
buttonWindow->paintButton();
return 0;
}

1 comment:

  1. Factory method design pattern is a creational design pattern in which creating an object is done using a factory method instead of calling a constructor.
    Abstract factory design pattern is used to create factories to create group of related objects.

    ReplyDelete