Schema Definition:
The factory method pattern defines an interface for creating objects, but subclasses decide which class to instantiate.
The factory method defers the instantiation of the class to the subclass.
Pattern Structure:
Creator is a class that implements all methods of manipulating a product, but does not implement a factory method.
All subclasses of creator must implement the Factory method (FactoryMethod ()) to actually produce the product.
All products must implement the product base class. Classes that use these products can then reference the base class, not the derived class.
Example:
The pizza shop wants to open some franchise stores.
Operators want to ensure the quality of franchise operations, so I hope these stores are using the code that can pass the practice of postgraduate examination. The problem is that every franchise may want to offer different flavors of pizza (say, New York). Chicago, California). This has been influenced by the location of the shop and the taste of pizza in the area.
WORKAROUND: Let each regional flavor of the pizza factory inherit the base class Pizza factory keeps the order system of the pizza factory unchanged. Then create your own flavor of the pizza. This really buys the pizza type and uses the detailed pizza factory to decide.
class diagram Design:
programming Implementation and operation results:
#include <iostream> #include <string> #include <list>using namespace std;
First create the Pizza class
Class Pizza{public:pizza (String Nam, String doug, string sauc) {name = Nam;dough = Doug;sauce = Sauc;} void Addtops (String tops) {Toppings.push_back (tops);} void Prepare () {cout << "Preparing" << name << endl;cout << "tossing dough" << endl;cout < ;< "Adding sauce" << endl;cout << "Adding toppings" << endl;list<string>::iterator iter = Toppi Ngs.begin (); for (; Iter!=toppings.end (); ++iter) {cout << "" << *iter;} cout << Endl;} void Bake () {cout << "bake for minutes at" << Endl;} void Cut () {cout << "cutting the pizza into diagonal slices" << endl;} void Box () {cout << "place pizza in offical pizzastore box" << Endl;} String GetName () {return name;} private:string name;string dough;string sauce;list<string> toppings;};
Then create the New York Cheese style Pizza class and the New York Clam Style Pizza class
Class Nystylecheesepizza:public Pizza{public:nystylecheesepizza ():P Izza ("NY Style Sauce and Cheese Pizza", "Thin crust D Ough "," Marinara Sauce ") {addtops (" grated Reggiano Cheese ");}; Class Nystyleclampizza:public Pizza{public:nystyleclampizza ():P Izza ("NY Style Sauce and Clam Pizza", "Thin crust dough", "Marinara Sauce") {addtops ("grated Clam");}};
Create a base class factory
Class Pizzastore{public:virtual ~pizzastore () {}pizza* Oderpizza (String type) {pizza* Pizza = Createpizza (type);p izza- >prepare ();p izza->bake ();p izza->cut ();p izza->box (); return pizza;} Virtual pizza* Createpizza (String type) {return NULL;}};
Create a detailed class factory (New York Pizza Plant)
Class Nypizzastore:public pizzastore{public:pizza* Createpizza (String item) {if (item = = "Cheese") {return new Nystylecheesepizza ();} else if (item = = "Clam") {return new Nystyleclampizza ();} else return null;}};/ /... Create other regional factories ...
Customer Code:
int main () {pizzastore* nystore = new Nypizzastore (); pizza* Pizza = Nystore->oderpizza ("cheese") cout << "Ethan ordered a" << pizza->getname () << Endl ; return 0;}
Operation Result:
Preparingny Style Sauce and Cheese Pizza
Tossingdough
Addingsauce
Addingtoppings
grated Reggiano Cheese
Bakefor minutes at 350
Cuttingthe Pizza into diagonal slices
Placepizza in offical pizzastore box
Ethanordered a NY Style Sauce and Cheese Pizza
Please press the random key to continue ...
Application of design principles: Design Principle 6: Dependency inversion principle (Dependency inversion priciple): To rely on abstraction, do not rely on the detail class.
Design principle 4: The factory method is used to process the creation of objects and encapsulate this behavior in subclasses. In this way, the code for the base class and the subclass object object creation code in the customer are decoupled.
Head First design mode
Design pattern C + + implementation--factory method mode