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();
break;
case Manager2:
result = new pepsi();
break;
default:
// Do whatever error logging you want
break;
}
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 {
public:
Widget* create_button() { return new MotifButton; }
Widget* create_menu() { return new MotifMenu; }
};
class WindowsFactory : public Factory {
public:
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() };
w[0]->draw();
w[1]->draw();
}
int main() {
factory = new MotifFactory;
factory = new WindowsFactory;
Widget* w = factory->create_button();
w->draw();
display_window_one();
display_window_two();
}
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 [email protected] 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.