工厂模式
工厂方法模式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; //抽象的水果类 class Fruit { public: virtual void getName() = 0; }; class Apple :public Fruit { public: virtual void getName() { cout << "我是苹果" << endl; } }; class Banana :public Fruit { public: virtual void getName() { cout << "我是香蕉 " << endl; } }; //添加一个梨产品 class Pear : public Fruit { public: virtual void getName() { cout << "我是鸭梨 " << endl; } }; //抽象的工厂类 class AbstractFactory { public: virtual Fruit * createFruit() = 0;//抽象的水果生产器 }; //苹果的工厂 class AppleFactory :public AbstractFactory { public: virtual Fruit * createFruit() { return new Apple; } }; //香蕉工厂 class BananaFactory : public AbstractFactory { public: virtual Fruit *createFruit() { return new Banana; } }; //添加梨的工厂 class PearFactory :public AbstractFactory { public: virtual Fruit *createFruit() { //.... return new Pear; } }; int main(void) { //1 给我来一个香蕉的工厂 AbstractFactory * bananaFactory = new BananaFactory; //2 给我来一个水果 Fruit * banana = bananaFactory->createFruit(); //只能够生成香蕉 //、多态 //Fruit *banana = new Banana; banana->getName(); //多态 delete bananaFactory; delete banana; AbstractFactory *appleFactory = new AppleFactory; Fruit *apple = appleFactory->createFruit(); apple->getName(); AbstractFactory *pearFactory = new PearFactory; Fruit *pear = pearFactory->createFruit(); //Fruit *pear = new Pear; pear->getName(); //多态 return 0; }
抽象的工厂方法模式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; //1 添加一个日本的工厂 和日本的苹果 和 香蕉 (添加一个产品族) //针对产品族进行添加 符合开闭 //2 添加一个产品种类, 鸭梨 (添加一个产品等级结构) //针对产品等级机构添加 不符合开闭 //3 考虑 1, 2 是否都符合开闭原则 class Fruit { public: virtual void getName() = 0; }; class USAApple :public Fruit { public: virtual void getName() { cout << "我是美国的苹果" << endl; } }; class USABanana :public Fruit { public: virtual void getName() { cout << "我是美国的香蕉" << endl; } }; class USAPear :public Fruit { public: virtual void getName() { cout << "我是美国的梨" << endl; } }; class ChinaApple :public Fruit { public: virtual void getName() { cout << "我是中国的苹果" << endl; } }; class ChinaBanana :public Fruit { public: virtual void getName() { cout << "我是中国的香蕉" << endl; } }; class ChinaPear :public Fruit { public: virtual void getName() { cout << "我是中国的梨" << endl; } }; //顶一个一个抽象的工厂,是面向产品族进行生产 class AbstractFactory { public: virtual Fruit *createApple() = 0; virtual Fruit *createBanana() = 0; //添加一个鸭梨的生成器 virtual Fruit* createPear() = 0; }; //美国的具体工厂 class USAFactory :public AbstractFactory { public: virtual Fruit *createApple() { return new USAApple; } virtual Fruit *createBanana() { return new USABanana; } //添加一个美国的生产器 virtual Fruit * createPear() { return new USAPear; } }; //中国的工厂 class ChinaFactory :public AbstractFactory { public: virtual Fruit *createApple() { return new ChinaApple; } virtual Fruit *createBanana() { return new ChinaBanana; } //添加一个的生产器 virtual Fruit * createPear() { return new ChinaPear; } }; //添加一个日本的产品族,工厂,和具体产品 符合 开闭原则 class JapanApple :public Fruit { public: virtual void getName() { cout << "我是日本的苹果" << endl; } }; class JapanBanana :public Fruit { public: virtual void getName() { cout << "我是日本的香蕉" << endl; } }; class JapanPear :public Fruit { public: virtual void getName() { cout << "我是日本的梨" << endl; } }; class JapanFactory :public AbstractFactory { public: virtual Fruit *createApple() { return new JapanApple; } virtual Fruit *createBanana() { return new JapanBanana; } virtual Fruit * createPear() { return new JapanPear; } }; int main(void) { //想要一个美国的香蕉 //1 来一个美国的工厂 AbstractFactory * usaF = new USAFactory; //2 来一个香蕉 Fruit *usaBanana = usaF->createBanana(); usaBanana->getName(); //想要一个中国的苹果 AbstractFactory *chinaF = new ChinaFactory; Fruit *chinaApple = chinaF->createApple(); chinaApple->getName(); return 0; }
抽象的工厂方法模式练习
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; // 0--------- 抽象层 ------ class CPU { public: virtual void caculate() = 0; }; class Card { public: virtual void display() = 0; }; class Memory { public: virtual void storage() = 0; }; class AbstractFactory { public: virtual CPU* createCPU() = 0; virtual Card* createCard() = 0; virtual Memory * createMem() = 0; }; //架构类 class Computer { public: Computer(CPU *cpu, Card *card, Memory *mem) { this->cpu = cpu; this->card = card; this->mem = mem; } void work() { this->cpu->caculate(); this->card->display(); this->mem->storage(); } private: CPU *cpu; Card*card; Memory *mem; }; //抽象层结束------ // ----- 实现层----- //intel厂商 class IntelCPU :public CPU { public: virtual void caculate() { cout << "Intel CPU 开始计算了..." << endl; } }; class IntelCard :public Card { public: virtual void display() { cout << "Intel card 开始显示 了..." << endl; } }; class IntelMem :public Memory { public: virtual void storage() { cout << "Intel mem 开始存储了..." << endl; } }; class IntelFactory :public AbstractFactory { public: virtual CPU* createCPU() { return new IntelCPU; } virtual Card* createCard() { return new IntelCard; } virtual Memory * createMem() { return new IntelMem; } }; // NVIDIA的厂商 class NvidiaCPU :public CPU { public: virtual void caculate() { cout << "Nvidia CPU 开始计算了..." << endl; } }; class NvidiaCard :public Card { public: virtual void display() { cout << "Nvidia card 开始显示 了..." << endl; } }; class NvidiaMem :public Memory { public: virtual void storage() { cout << "Nvidia mem 开始存储了..." << endl; } }; class NvidiaFacory :public AbstractFactory { public: virtual CPU* createCPU() { return new NvidiaCPU; } virtual Card* createCard() { return new NvidiaCard; } virtual Memory * createMem() { return new NvidiaMem; } }; // Kinston 厂商 class KinstonCPU :public CPU { public: virtual void caculate() { cout << "Kinston CPU 开始计算了..." << endl; } }; class KinstonCard :public Card { public: virtual void display() { cout << "Kinston card 开始显示 了..." << endl; } }; class KinstonMem :public Memory { public: virtual void storage() { cout << "Kinston mem 开始存储了..." << endl; } }; class KinstonFacory :public AbstractFactory { public: virtual CPU* createCPU() { return new KinstonCPU; } virtual Card* createCard() { return new KinstonCard; } virtual Memory * createMem() { return new KinstonMem; } }; int main(void) { //1 组装一个intel系列的电脑 对intel公司的所有产品,执行一个单元测试。 //创建一个intel的工厂 AbstractFactory * intelFactory = new IntelFactory; CPU *intelCPU = intelFactory->createCPU(); Card *intelCard = intelFactory->createCard(); Memory *intelMem = intelFactory->createMem(); Computer *com1 = new Computer(intelCPU, intelCard, intelMem); com1->work(); cout << " ----- " << endl; //2 组装一个 intel的cpu kinston card NVIDIA的显卡的电脑 AbstractFactory *kinstonFactory = new KinstonFacory; Memory *kingstonMem = kinstonFactory->createMem(); AbstractFactory *nvidiaFactory = new NvidiaFacory; Card *nvidiaCard = nvidiaFactory->createCard(); Computer *com2 = new Computer(intelCPU, nvidiaCard, kingstonMem); com2->work(); return 0; }