文章标题

xiaoxiao2021-02-28  110

//工厂:将对象的创建过程封装起来,以便将代码从具体类中解耦。 //参考HeadFirst 设计模式, 从java版本改写到C++版本。 //优缺点参考自:http://www.cnblogs.com/cxjchen/p/3143633.html //主要是披萨店

1 简单工厂模式 /*简单工厂模式之参考学习: 他的主要特点是需要在工厂类中做判断,从而创造相应的产品,当增加新的产品时,就需要修改工厂类。 将原代码中的new操作符替换成工厂对象的创建方法。 优点 1.隐藏了对象创建的细节,将产品的实例化推迟到子类中实现。 2.客户端基本不用关心使用的是哪个产品,只需要知道用哪个工厂就行了,提供的类型也可以用比较便于识别的字符串。 3.方便添加新的产品子类,每次只需要修改工厂类传递的类型值就行了。 4.遵循了依赖倒转原则。 缺点 1.要求产品子类的类型差不多,使用的方法名都相同,如果类比较多,而所有的类又必须要添加一种方法,则会是非常麻烦的事情。或者是一种类另一种类有几种方法不相同,客户端无法知道是哪一个产品子类,也就无法调用这几个不相同的方法。 2.每添加一个产品子类,都必须在工厂类中添加一个判断分支,这违背了开放-封闭原则。 */

#include <iostream> #include <string> using namespace std; //披萨的抽象类 class Pizza { public: Pizza(){} virtual ~Pizza(){} void prepare(){ cout << "Preapring: " + name << endl; } void back(){ cout << "Backing for 25 minutes" << endl; } void box(){ cout << "Placing Pizza in a box" << endl; } string getname(){ return name; } protected: string name; }; class ChesszePizza : public Pizza { public: ChesszePizza(){ name = "ChesszePizza"; cout << "this is a chesszePizza" << endl; } }; class ClamPizza : public Pizza { public: ClamPizza(){ name = "ClamPizza"; cout << "this is a ClamPizza" << endl; } }; //建立一个简单的披萨工厂 class AbstractFactory { public: virtual Pizza* creatPizza(string type) = 0; }; class SimplePizzaFactory : public AbstractFactory {//将对象的创建放在一个类中 public: Pizza* creatPizza(string type){//注意这里返回的是指针 Pizza *pizza = NULL; if (type == "Cheese"){ pizza = new ChesszePizza(); } else if (type == "Clam"){ pizza = new ClamPizza(); } return pizza; } }; //建立一个商店 class PizzaStore { public: SimplePizzaFactory* factory; PizzaStore(SimplePizzaFactory* factory){ this->factory = factory; } Pizza* orderPizza(string type){ Pizza *pizza; pizza = factory->creatPizza(type); pizza->back(); pizza->box(); return pizza; } }; int main() { SimplePizzaFactory* f = new SimplePizzaFactory(); PizzaStore *ps = new PizzaStore(f); Pizza* pizza=ps->orderPizza("Cheese"); cout << pizza->getname() << endl; system("pause"); return 0; }

2 工厂模式: //工厂:将对象的创建过程封装起来,以便将代码从具体类中解耦。 /*工厂模式之参考学习: 优点 基本与简单工厂模式一致,多的一点优点就是遵循了开放-封闭原则,使得模式的灵活性更强。

缺点 与简单工厂模式差不多。 */

#include <iostream> #include <string> using namespace std; //披萨的抽象类 class Pizza { public: Pizza(){} virtual ~Pizza(){} void prepare(){ cout << "Preapring: " + name << endl; } void back(){ cout << "Backing for 25 minutes" << endl; } void box(){ cout << "Placing Pizza in a box" << endl; } string getname(){ return name; } protected: string name; }; class NYChesszePizza : public Pizza { public: NYChesszePizza(){ name = "ChesszePizza"; cout << "this is a NYchesszePizza" << endl; } }; class NYClamPizza : public Pizza { public: NYClamPizza(){ name = "ClamPizza"; cout << "this is a NYClamPizza" << endl; } }; class ChaChesszePizza : public Pizza { public: ChaChesszePizza(){ name = "ChesszePizza"; cout << "this is a ChachesszePizza" << endl; } }; class ChaClamPizza : public Pizza { public: ChaClamPizza(){ name = "ClamPizza"; cout << "this is a ChaClamPizza" << endl; } }; //建立一个抽象的商店工厂 class PizzaStore { public: Pizza* orderPizza(string type){ Pizza *pizza; pizza =creatPizza(type); pizza->back(); pizza->box(); return pizza; } protected: virtual Pizza* creatPizza(string type) = 0; }; class NYPizzaStore : public PizzaStore { Pizza* creatPizza(string type){ Pizza *pizza = NULL; if (type == "Cheese"){ pizza = new NYChesszePizza(); } else if (type == "Clam"){ pizza = new NYClamPizza(); } return pizza; } }; class ChaPizzastore : public PizzaStore { Pizza* creatPizza(string type){ Pizza *pizza = NULL; if (type == "Cheese"){ pizza = new ChaChesszePizza(); } else if (type == "Clam"){ pizza = new ChaClamPizza(); } return pizza; } }; int main() { PizzaStore *nyStore = new NYPizzaStore(); PizzaStore *chaStore = new ChaPizzastore(); Pizza *p = nyStore->orderPizza("Cheese"); cout << endl; Pizza *p1 = chaStore->orderPizza("Clam"); system("pause"); return 0; }

[图片] //工厂:将对象的创建过程封装起来,以便将代码从具体类中解耦。 /*抽象工厂模式之参考学习: 抽象工厂模式就变得比工厂模式更为复杂,就像上面提到的缺点一样,工厂模式和简单工厂模式要求产品子类必须要是同一类型的,拥有共同的方法,这就限制了产品子类的扩展。于是为了更加方便的扩展,抽象工厂模式就将同一类的产品子类归为一类,让他们继承同一个抽象子类,我们可以把他们一起视作一组,然后好几组产品构成一族。 此时,客户端要使用时必须知道是哪一个工厂并且是哪一组的产品抽象类。每一个工厂子类负责产生一族产品,而子类的一种方法产生一种类型的产品。 优点 1.封装了产品的创建,使得不需要知道具体是哪种产品,只需要知道是哪个工厂就行了。 2.可以支持不同类型的产品,使得模式灵活性更强。 3.可以非常方便的使用一族中间的不同类型的产品。

缺点 1.结构太过臃肿,如果产品类型比较多,或者产品族类比较多,就会非常难于管理。 2.每次如果添加一组产品,那么所有的工厂类都必须添加一个方法,这样违背了开放-封闭原则。所以一般适用于产品组合产品族变化不大的情况。 */ //这里在前面的基础上进行改写,其中原料采用抽象工厂、商店还是工厂模式。

#include <iostream> #include <string> using namespace std; //披萨原料 class Dough { public: Dough(){ dough_name = "yuanshi"; } virtual ~Dough(){} string dough_name; }; class NYDough : public Dough { public: NYDough(){ dough_name = "NY_dough"; cout << "choose NY_dough" << endl; } ~NYDough(){} }; class ChaDough : public Dough { public: ChaDough(){ dough_name = "Cha_dough"; cout << "choose Cha_dough" << endl; } ~ChaDough(){} }; //披萨的抽象类 class Pizza { public: Pizza(){} virtual ~Pizza(){} virtual void prepare(){ cout << "Preapring: " + name << endl; } void back(){ cout << "Backing for 25 minutes" << endl; } void box(){ cout << "Placing Pizza in a box" << endl; } string getname(){ return name; } protected: string name; Dough* dough;//某种原料 }; //建造原料的工厂 class Ingredientfactory { public: Ingredientfactory(){} virtual ~Ingredientfactory(){} virtual Dough* creatDough() = 0; }; class NYIngredientfactory : public Ingredientfactory { public: Dough* creatDough(){ return new NYDough(); } }; class ChaIngredientfactory : public Ingredientfactory { public: Dough* creatDough(){ return new ChaDough(); } }; class NYChesszePizza : public Pizza { public: Ingredientfactory* ingredientfactory; NYChesszePizza(Ingredientfactory* ingredientfactory){ this->ingredientfactory = ingredientfactory; name = "ChesszePizza"; cout << "this is a NYchesszePizza" << endl; } void prepare(){ dough = ingredientfactory->creatDough(); } }; class NYClamPizza : public Pizza { public: Ingredientfactory* ingredientfactory; NYClamPizza(Ingredientfactory* ingredientfactory){ this->ingredientfactory = ingredientfactory; name = "ClamPizza"; cout << "this is a NYClamPizza" << endl; } void prepare(){ dough = ingredientfactory->creatDough(); } }; class ChaChesszePizza : public Pizza { public: Ingredientfactory* ingredientfactory; ChaChesszePizza(Ingredientfactory* ingredientfactory){ this->ingredientfactory = ingredientfactory; name = "ChesszePizza"; cout << "this is a ChachesszePizza" << endl; } void prepare(){ dough = ingredientfactory->creatDough(); } }; class ChaClamPizza : public Pizza {//具体的披萨传入一个参数,用于指出所用的原材料工厂 public: Ingredientfactory* ingredientfactory; ChaClamPizza(Ingredientfactory* ingredientfactory){ this->ingredientfactory = ingredientfactory; name = "ClamPizza"; cout << "this is a ChaClamPizza" << endl; } void prepare(){ dough = ingredientfactory->creatDough(); } }; //建立一个抽象的商店工厂 class PizzaStore { public: Pizza* orderPizza(string type){ Pizza *pizza; pizza = creatPizza(type); pizza->prepare(); pizza->back(); pizza->box(); return pizza; } protected: virtual Pizza* creatPizza(string type) = 0; }; class NYPizzaStore : public PizzaStore { Pizza* creatPizza(string type){ Pizza *pizza = NULL; Ingredientfactory * ing = new NYIngredientfactory(); if (type == "Cheese"){ pizza = new NYChesszePizza(ing); } else if (type == "Clam"){ pizza = new NYClamPizza(ing); } return pizza; } }; class ChaPizzastore : public PizzaStore { Pizza* creatPizza(string type){ Pizza *pizza = NULL; Ingredientfactory * ing = new ChaIngredientfactory(); if (type == "Cheese"){ pizza = new ChaChesszePizza(ing); } else if (type == "Clam"){ pizza = new ChaClamPizza(ing); } return pizza; } }; int main() { PizzaStore *nyStore = new NYPizzaStore(); PizzaStore *chaStore = new ChaPizzastore(); Pizza *p = nyStore->orderPizza("Cheese"); cout << endl; Pizza *p1 = chaStore->orderPizza("Clam"); system("pause"); return 0; }

[图片] //第一次写博客(一个小目标),尝试一下。

转载请注明原文地址: https://www.6miu.com/read-60932.html

最新回复(0)