Mode definition:
The factory method mode defines an interface for creating objects, but the subclass determines the class to be instantiated. The factory method delays the class Instantiation to the subclass.
Mode structure:
Creator is a class that implements all the methods for manipulating products, but does not implement factory methods. All sub-classes of the Creator must implement the factory method (factoryMethod () to produce the product.
All products must implement the Product base class, so that the class of these products can reference this base class, rather than the derived class.
Example:
Pizza shops hope to open some franchisees. The operator wants to ensure the quality of the operation of the franchise stores, so they all want to use the code that can be tested and tested. The problem is that each franchise store may want to provide different flavors of pizza (for example, New York, Chicago, and California), which is affected by the location where the store is opened and the taste of the pizza in the region. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + kernel/kernel + fOtrXExfvI + KGj1eLR + kernel + md1_m2jrmq508o + 38 zltcTF + kernel + cjxpbwcgc3jjjpq = "http://www.2cto.com/uploadfile/Collfiles/20140607/20140607090951261.jpg" alt = "\">
Programming implementation and execution results:
#include
#include
#include
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
::iterator iter = toppings.begin();for(; iter!=toppings.end(); ++iter){cout << ""<< *iter;}cout << endl;}void bake(){cout << "Bake for 25 minutes at 350" << 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
toppings;};
Then create the New York cheese style pizza class and the New York clam style pizza class.
class NYStyleCheesePizza : public Pizza{public:NYStyleCheesePizza():Pizza("NY Style Sauce and Cheese Pizza", "Thin Crust Dough", "Marinara Sauce"){addTops("Grated Reggiano Cheese");}};class NYStyleClamPizza : public Pizza{public:NYStyleClamPizza():Pizza("NY Style Sauce and Clam Pizza", "Thin Crust Dough", "Marinara Sauce"){addTops("Grated Clam");}};
Create a base Factory
class PizzaStore{public:virtual ~PizzaStore(){}Pizza* oderPizza(string type){Pizza* pizza = createPizza(type);pizza->prepare();pizza->bake();pizza->cut();pizza->box();return pizza;}virtual Pizza* createPizza(string type){return NULL;}};
Create a specific factory (New York pizza Factory)
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 factories in other regions...
Customer Code:
int main(){PizzaStore* nyStore = new NYPizzaStore();Pizza* pizza = nyStore->oderPizza("cheese");cout << "Ethan ordered a "<< pizza->getName() << endl;return 0;}
Execution result:
PreparingNY Style Sauce and Cheese Pizza
Tossingdough
Addingsauce
Addingtoppings
Grated Reggiano Cheese
Bakefor 25 minutes at 350
Cuttingthe pizza into diagonal slices
Placepizza in offical PizzaStore box
Ethanordered a NY Style Sauce and Cheese Pizza
Press any key to continue...
Application of Design Principles: Design Principle 6: Dependency Inversion principle (Dependency Inversion Priciple): To rely on abstraction, do not rely on specific classes.
Design Principle 4: The Factory method is used to process object creation and encapsulate such behavior in sub-classes. In this way, the customer's code for the base class is decoupled from the code for creating the subclass object.
Refer to Head First design mode