- Back to Home »
- Factory Pattern
Factory
Patterns:
Creational patterns: Abstract the object instantiation process. They hide how objects are
created and help
make the overall system independent of how its
objects are
created and composed.
be instantiated.
instantiation to
another object.
The
Factory method pattern:
“Define an interface for creating an object but let subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclasses.”
Applicability:
Use the Factory Method pattern in any of
the following situations:
v A class can't anticipate the class of objects it must create
v A class wants its subclasses to specify the objects it creates
Structure:
Product : Defines the interface for the type of objects the factory method creates
ConcreteProduct: Implements the Product interface
Creator: Declares the factory method, which returns an object of type Product
ConcreteCreator: Overrides the factory method to return an instance of concreteProduct
The Factory Method Pattern lets
subclasses decide which class to instantiate?
It
means that Creator class is written without knowing what actual ConcreteProduct
class will be instantiated. The ConcreteProduct class which is instantiated is
determined solely by which ConcreteCreator subclass is instantiated and used by
the application.
Example:
#include <iostream>
using namespace std;
class product
{
public:
product()
{
cout
<< "product constructor" << endl;
}
virtual void displayname()
= 0;
};
class concreteprd1 : public product
{
public:
concreteprd1()
{
cout
<< "concreteprd1
constructor" << endl;
}
void displayname()
{
cout
<< "the concrete product 1" << endl;
}
};
class concreteprd2 : public product
{
public:
concreteprd2()
{
cout
<< "concreteprd2
constructor" << endl;
}
void displayname()
{
cout
<< "the concrete product 2" << endl;
}
};
class creator
{
public:
virtual product* createproduct()
= 0;
};
class concretecreator1 : public creator
{
public:
virtual product* createproduct()
{
cout
<< "concrete creator 1 " << endl;
return new concreteprd1;
}
};
class concretecreator2 : public creator
{
public:
virtual product* createproduct()
{
cout
<< "concrete creator 2 " << endl;
return new concreteprd2;
}
};
int main() {
cout <<
"Factory Pattern" << endl; //
prints Factory Pattern
creator *c = new concretecreator1();
product *p =
c->createproduct();
p->displayname();
delete c;
delete p;
return 0;
}
=======================================================================
Simple Factory Example
//
// Interface
//
public interface IChoice
{
string Buy();
}
//
// Factory Class
//
public class FactoryChoice
{
static public IChoice getChoiceObj(string cChoice)
{
IChoice objChoice=null;
if (cChoice.ToLower() == "car")
{
objChoice = new clsCar();
}
else if (cChoice.ToLower() == "bike")
{
objChoice = new clsBike();
}
else
{
objChoice = new InvalidChoice();
}
return objChoice;
}
}
//Business classes
//Car
public class clsBike:IChoice
{
#region IChoice Members
public string Buy()
{
return ("You choose Bike");
}
#endregion
}
//Bike
public class clsCar:IChoice
{
#region IChoice Members
public string Buy()
{
return ("You choose Car");
}
#endregion
}
//Client class
IChoice objInvoice;
objInvoice = FactoryClass.FactoryChoice.getChoiceObj(txtChoice.Text.Trim());
MessageBox.Show(objInvoice.Buy());