标签:
工厂模式:定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个。工厂方法让类把实例化推迟到子类。——《HEAD FIRST 设计模式》
我的c++代码:
#ifndef DESIGN_FACTORY_H_ #define DESIGN_FACTORY_H_ #include <string> #include <vector> class Pizza { public: void prepare(); void bake(); void cut(); void box(); protected: std::string m_name; std::string m_dough; std::string m_sauce; std::vector<std::string> m_toppings; }; class PizzaStore { public: virtual Pizza* createPizza(std::string t) = 0; public: Pizza* orderPizza(std::string t); }; ///////////////////////////////////// class NYStylePizzaStore:public PizzaStore { public: virtual Pizza* createPizza(std::string t); }; class ChicagoStylePizzaStore:public PizzaStore { public: virtual Pizza* createPizza(std::string t); }; ///////////////////////////////////// class NYStyleCheesePizza:public Pizza { public: NYStyleCheesePizza(); }; class NYStyleVeggiePizza:public Pizza { public: NYStyleVeggiePizza(); }; class NYStyleClamPizza:public Pizza { public: NYStyleClamPizza(); }; class NYStylePepperoniPizza:public Pizza { public: NYStylePepperoniPizza(); }; class ChicagoStyleCheesePizza:public Pizza { public: ChicagoStyleCheesePizza(); }; class ChicagoStyleVeggiePizza:public Pizza { public: ChicagoStyleVeggiePizza(); }; class ChicagoStyleClamPizza:public Pizza { public: ChicagoStyleClamPizza(); }; class ChicagoStylePepperoniPizza:public Pizza { public: ChicagoStylePepperoniPizza(); }; #endif // DESIGN_FACTORY_H_
#include "factory.h" #include <iostream> void Pizza::prepare() { std::cout << "Preparing " << this->m_name << std::endl; std::cout << "Tossing dough..." << std::endl; std::cout << "Adding sauce..." << std::endl; std::cout << "Adding toppings:" << std::endl; std::vector<std::string>::iterator it = this->m_toppings.begin(); for(; it != this->m_toppings.end(); ++it) { std::cout << *it << std::endl; } } void Pizza::bake() { std::cout << "Bake for 25 minutes at 350" << std::endl; } void Pizza::cut() { std::cout << "Cutting the pizza into diagonal slices" << std::endl; } void Pizza::box() { std::cout << "Place pizza in official PizzaStore box" << std::endl; } Pizza* PizzaStore::orderPizza(std::string t) { Pizza* r = createPizza(t); r->prepare(); r->bake(); r->box(); r->cut(); return r; } Pizza* NYStylePizzaStore::createPizza(std::string t) { if (t == "cheese") { return new NYStyleCheesePizza(); } else if (t == "veggie") { return new NYStyleVeggiePizza(); } else if (t == "clam") { return new NYStyleClamPizza(); } else if (t == "pepperoni") { return new NYStylePepperoniPizza(); } else { return NULL; } } Pizza* ChicagoStylePizzaStore::createPizza(std::string t) { if (t == "cheese") { return new ChicagoStyleCheesePizza(); } else if (t == "veggie") { return new ChicagoStyleVeggiePizza(); } else if (t == "clam") { return new ChicagoStyleClamPizza(); } else if (t == "pepperoni") { return new ChicagoStylePepperoniPizza(); } else { return NULL; } } NYStyleCheesePizza::NYStyleCheesePizza() { this->m_name = "NY Style Sauce and Cheese Pizza"; this->m_dough = "Thin Crust Dough"; this->m_sauce = "Marinara Sauce"; this->m_toppings.push_back("Grated Reggiano Chees"); } NYStyleVeggiePizza::NYStyleVeggiePizza() { this->m_name = "NY Style Veggie Pizza"; this->m_dough = "Thin Crust Dough"; this->m_sauce = "Marinara Sauce"; this->m_toppings.push_back("Grated Reggiano Cheese"); this->m_toppings.push_back("Garlic"); this->m_toppings.push_back("Onion"); this->m_toppings.push_back("Mushrooms"); this->m_toppings.push_back("Red Pepper"); } NYStyleClamPizza::NYStyleClamPizza() { this->m_name = "NY Style Clam Pizza"; this->m_dough = "Thin Crust Dough"; this->m_sauce = "Marinara Sauce"; this->m_toppings.push_back("Grated Reggiano Cheese"); this->m_toppings.push_back("Fresh Clams from Long Island Sound"); } NYStylePepperoniPizza::NYStylePepperoniPizza() { this->m_name = "NY Style Pepperoni Pizza"; this->m_dough = "Thin Crust Dough"; this->m_sauce = "Marinara Sauce"; this->m_toppings.push_back("Grated Reggiano Cheese"); this->m_toppings.push_back("Sliced Pepperoni"); this->m_toppings.push_back("Garlic"); this->m_toppings.push_back("Onion"); this->m_toppings.push_back("Mushrooms"); this->m_toppings.push_back("Red Pepper"); } ChicagoStyleCheesePizza::ChicagoStyleCheesePizza() { this->m_name = "Chicago Style Deep Dish Cheese Pizza"; this->m_dough = "Extra Thick Crust Dough"; this->m_sauce = "Plum Tomato Sauce"; this->m_toppings.push_back("Shredded Mozzarella Cheese"); } ChicagoStylePepperoniPizza::ChicagoStylePepperoniPizza() { this->m_name = "Chicago Style Pepperoni Pizza"; this->m_dough = "Extra Thick Crust Dough"; this->m_sauce = "Plum Tomato Sauce"; this->m_toppings.push_back("Shredded Mozzarella Cheese"); this->m_toppings.push_back("Black Olives"); this->m_toppings.push_back("Spinach"); this->m_toppings.push_back("Eggplant"); this->m_toppings.push_back("Sliced Pepperoni"); } ChicagoStyleClamPizza::ChicagoStyleClamPizza() { this->m_name = "Chicago Style Clam Pizza"; this->m_dough = "Extra Thick Crust Dough"; this->m_sauce = "Plum Tomato Sauce"; this->m_toppings.push_back("Shredded Mozzarella Cheese"); this->m_toppings.push_back("Frozen Clams from Chesapeake Bay"); } ChicagoStyleVeggiePizza::ChicagoStyleVeggiePizza() { this->m_name = "Chicago Deep Dish Veggie Pizza"; this->m_dough = "Extra Thick Crust Dough"; this->m_sauce = "Plum Tomato Sauce"; this->m_toppings.push_back("Shredded Mozzarella Cheese"); this->m_toppings.push_back("Black Olives"); this->m_toppings.push_back("Spinach"); this->m_toppings.push_back("Eggplant"); }
标签:
原文地址:http://www.cnblogs.com/foolbread/p/4453021.html