抽象工厂模式
Posted mathyk
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了抽象工厂模式相关的知识,希望对你有一定的参考价值。
抽象工厂模式是所有形态的工厂模式中最为抽象和最其一般性的。抽象工厂模式可以向客户端提供一个接口,使其客户端在不必指定产品的具体类型情况下,能够创建多个产品族的产品对象。
工厂方法模式针对的是一个产品等级结构,而抽象工厂模式针对的是多个产品等级结构(产品族)。
//抽象工厂 和 一般工厂模式相比较的话,产品的细化……
class Fruit { public: virtual void sayName() { std::cout<<"Fruit"<<std::endl; } }; class FruitFactory { public: virtual Fruit *getBanana() { std::cout<<"FruitFactory:getBanana"<<std::endl; return NULL; } virtual Fruit *getApple() { std::cout<<"FruitFactory:getApple"<<std::endl; return NULL; } virtual Fruit *getMg() { std::cout<<"FruitFactory:getMg"<<std::endl; return NULL; } }; class SouthBanana : public Fruit { public: virtual void sayName() { std::cout<<"我是SouthBanana"<<std::endl; } }; class SouthApple : public Fruit { public: virtual void sayName() { std::cout<<"我是SouthApple"<<std::endl; } }; class SouthMg : public Fruit { public: virtual void sayName() { std::cout<<"我是SouthMg"<<std::endl; } }; class NorthBanana : public Fruit { public: virtual void sayName() { std::cout<<"我是NorthBanana"<<std::endl; } }; class NorthApple : public Fruit { public: virtual void sayName() { std::cout<<"我是NorthApple"<<std::endl; } }; class NorthMg : public Fruit { public: virtual void sayName() { std::cout<<"我是NorthMg"<<std::endl; } }; //NorthFactory class NorthFactory :public FruitFactory { public: virtual Fruit *getBanana() { return new NorthBanana(); } virtual Fruit *getApple() { return new NorthApple(); } virtual Fruit *getMg() { return new NorthMg(); } }; //SouthFactory class SouthFactory :public FruitFactory { public: virtual Fruit *getBanana() { return new SouthBanana(); } virtual Fruit *getApple() { return new SouthApple(); } virtual Fruit *getMg() { return new SouthMg(); } };
以上是关于抽象工厂模式的主要内容,如果未能解决你的问题,请参考以下文章