"Factory Method Design pattern" is used when many derived classes are derived from a common base class and object of which derived class should be instantiated is only known at run time.
One way to achieve it by providing a static method in base class itself which returns pointer to the same base class.
When client needs to create object it calls this static method to create object of required derived class by passing type of subclass as string.And need not instantiate specific object by using subclass constructor.
Benefit of factory design pattern is that when new type is added then changes required only at one place where object is created ie only new case is added in factory method.
Intent of factory method is to provide a method to create objects, But defer the object creation to derive class based on some condition or configuration which acts like a virtual constructor. It decouples client from object creation.
Another variation of Factory pattern is "Abstract factory pattern".
In abstract factory design pattern an interface(Abstract base class) is provided to create object, But, let the subclass to decide which class to instantiate.
In example ShapeFactory is the abstract base class. And, object is created based on type passed.
Factory design pattern: We have factory that produces objects that from a particular base class.
Abstract Factory Design Pattern: We have factory that creates other factories and these factories in turn creates objects derived from other base class. Abstract factory needed when when we want to create collection of related objects and not just one object.
"Factory Method Design pattern" Implementation in C plus plus
#include<iostream>
using namespace std;
class Shape
{
public:
virtual void whichShape()=0;
static Shape* getShape(string s);//Factory method to create Object
};
class Rectangle:public Shape
{
public:
void whichShape(){cout<<"Rectangle"<<endl;}
};
class Circle:public Shape
{
public:
void whichShape(){cout<<"Circle"<<endl;}
};
Shape* Shape::getShape(string s)
{
if(s=="Rectangle")
return new Rectangle();
if(s=="Circle")
return new Circle();
}
int main()
{
//At run time requested type is Rectangle
Shape *s1= Shape::getShape("Rectangle");
s1->whichShape();
//At run time requested type is Circle
Shape *s2= Shape::getShape("Circle");
s2->whichShape();
return 0;
}
//---------------------------
"Abstract Factory Design pattern" Implementation in C++
#include<iostream>
using namespace std;
class Shape
{
public:
virtual void whichShape()=0;
// static Shape* getShape(string s);
};
class Rectangle:public Shape
{
public:
void whichShape(){cout<<"Rectangle"<<endl;}
};
class Circle:public Shape
{
public:
void whichShape(){cout<<"Circle"<<endl;}
};
class ShapeFactory
{
public:
static Shape* getShape(string s);//Factory1
static Shape* getEdgyShape(string s);//Factory2
static Shape* getRoundedShape(string s);//Factory3
};
Shape* ShapeFactory::getShape(string s)
{
if(s=="Rectangle")
return new Rectangle();
if(s=="Circle")
return new Circle();
}
int main()
{
//At run time requested type is Rectangle
ShapeFactory* sf = new ShapeFactory();
Shape* s1 = sf->getShape("Rectangle");
s1->whichShape();
//At run time requested type is Circle
ShapeFactory* sf2 = new ShapeFactory();
Shape* s2 = sf2->getShape("Circle");
s2->whichShape();
return 0;
}
Showing posts with label Factory. Show all posts
Showing posts with label Factory. Show all posts
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. 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;
}
Subscribe to:
Posts (Atom)