C++設計模式——工廠方法模式和抽象工廠方法模式
阿新 • • 發佈:2018-12-19
一、工廠方法模式
上面的簡單工廠模式的缺點是當新增產品的時候就要去修改工廠的類,這就違反了開放封閉原則,(類、模組、函式)可以擴充套件,但是不可以修改,於是,就出現了工廠方法模式。所謂工廠方法模式,是指定義一個用於建立物件的介面,讓子類決定例項化哪一個類。
#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; }
二、抽象工廠模式
為什麼要有抽象工廠模式,假如我們A產品中有A1和A2兩種型號的廠品,B產品中有B1和B2兩種型號的廠品,那怎麼辦,上面兩種工廠模式就不能解決了。這個時候抽象工廠模式就登場了。還是開設兩家工廠,工廠A負責生產A1 、A2型號產品,B工廠負責生產B1、B2型號的產品。
提供一個建立一系列相關或相互依賴物件的介面,而無需指定它們具體的類。 適用性:一個系統要獨立於它的產品的建立、組合和表示時。一個系統要由多個產品系列中的一個來配置時。當你要強調一系列相關的產品物件的設計以便進行聯合使用時。當你提供一個產品類庫,而只想顯示它們的介面而不是實現時。
#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; }
抽象工廠模式練習
#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; }