设计模式之十一:抽象工厂模式(Abstract Factory)
生活随笔
收集整理的這篇文章主要介紹了
设计模式之十一:抽象工厂模式(Abstract Factory)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
抽象工廠模式:
提供了一個創建一系列相關的或相互依賴的對象的接口而不須要詳細指定它們的類型。
UML類圖:
主要包含:
主要的抽象工廠模式C++代碼例如以下:
#include <iostream> #include <stdlib.h> #include <stdio.h>class AbstractProductA {public:virtual void show()=0; };class ProductA1:public AbstractProductA {public:void show(){std::cout<<"ProductA1::show()"<<std::endl;}};class ProductA2:public AbstractProductA {public:void show(){std::cout<<"ProductA2::show()"<<std::endl;} };class AbstractProductB {public :virtual void display()=0; };class ProductB1:public AbstractProductB {public:void display(){std::cout<<"ProductB1::display()"<<std::endl;} };class ProductB2:public AbstractProductB {public:void display(){std::cout<<"ProductB2::display()"<<std::endl;} };class AbstractFactory {public:virtual AbstractProductA* createProductA()=0;virtual AbstractProductB* createProductB()=0;};class ConcreteFactory1:public AbstractFactory {public:AbstractProductA *createProductA(){return new ProductA1();}AbstractProductB *createProductB(){return new ProductB1();} };class ConcreteFactory2:public AbstractFactory {public:AbstractProductA *createProductA(){return new ProductA2();}AbstractProductB *createProductB(){return new ProductB2();} };class Client {public:Client(){}Client(AbstractFactory *af){abstractFactory=af;}AbstractProductA * getProductA(){return abstractProductA;}AbstractProductB* getProductB(){return abstractProductB;}void run(){abstractProductA=abstractFactory->createProductA();abstractProductB=abstractFactory->createProductB(); }~Client(){if(!abstractProductA)delete abstractProductA;if(!abstractProductB)delete abstractProductB;}private:AbstractFactory *abstractFactory;AbstractProductA * abstractProductA;AbstractProductB * abstractProductB; };int main() {std::cout<<"抽象工廠模式測試"<<std::endl;AbstractFactory *af=new ConcreteFactory1();Client* client=new Client(af);client->run();client->getProductA()->show();client->getProductB()->display();delete af;delete client;return 0; }運行結果:
再看一個詳細的樣例:
大陸上有食肉動物和食草動物,可是不同的大陸比方說非洲大陸的食肉動物和食草動物和美洲大陸的食肉動物和食草動物是不同的。抽象工廠工廠模式和工廠方法模式的不同點就在于抽象工廠模式有兩處變化的地方,除了工廠類能夠變化外,抽象的產品類有多個而不是一個。
UML圖例如以下:
C++代碼實現例如以下:
#include <stdlib.h> #include <stdio.h> #include <iostream>//食草動物 class Herbivore {public:virtual void drink()=0; };//羚羊的一種。產于非洲 class Wildebeest:public Herbivore {public:void drink(){std::cout<<"wildbeest drink"<<std::endl;} };//美洲野牛 class Bison:public Herbivore {public:void drink(){std::cout<<"bison drink"<<std::endl;} };//食肉動物 class Carnivore {public:virtual void eat()=0; };//非洲獅子 class Lion:public Carnivore {public:void eat(){std::cout<<"lion eat"<<std::endl;} };//狼 class Wolf:public Carnivore {public:void eat(){std::cout<<"wolf eat"<<std::endl;} };//大陸工廠 class ContinentFactory {public:virtual Herbivore * createHerbivore()=0;virtual Carnivore * createCarnivore()=0;};class AfricaFactory :public ContinentFactory {public:virtual Herbivore * createHerbivore(){return new Wildebeest();}virtual Carnivore * createCarnivore(){return new Lion();}};class AmericaFactory :public ContinentFactory {public:virtual Herbivore * createHerbivore(){return new Bison();}virtual Carnivore * createCarnivore(){return new Wolf();}};class AnimalWorld {public:AnimalWorld(){}AnimalWorld(ContinentFactory *cf){herbivore=cf->createHerbivore();carnivore=cf->createCarnivore();}void action(){herbivore->drink();carnivore->eat();}~AnimalWorld(){if(!herbivore)delete herbivore;if(!carnivore)delete carnivore;}private:Herbivore * herbivore;Carnivore * carnivore;};int main() {std::cout<<"抽象工廠模式詳細的樣例"<<std::endl;ContinentFactory * cf=new AmericaFactory;AnimalWorld *an=new AnimalWorld(cf);an->action();delete cf;delete an;return 0; }運行結果:
總結
以上是生活随笔為你收集整理的设计模式之十一:抽象工厂模式(Abstract Factory)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: javascript 漂亮随机码
- 下一篇: 百度首页增加了二维码扫描