創造型設計模式-----抽象工廠模式
阿新 • • 發佈:2017-12-25
sin mage pub gin bubuko 概念 .cpp col alt
一種商品需要用多個產品組成就需要運用抽象工廠模式。
概念:
抽象工廠:聲明一個用於完成抽象商品對象創建操作的接口
具體工廠:實現創建具體產品對象的操作
抽象產品:聲明一個用於一類產品對象的接口
具體產品:定義有相應的具體工廠來創建的產品對象
客戶端:使用抽象工廠和抽象產品類聲明的唯一接口
如下圖所示:抽象工廠UML圖
舉個例子:車與零件
車有許許多多的零件part組成:以wheels(輪胎),mirrors(車鏡),engine(引擎),body(車身)組成,不同的車需要不同的零件配置
代碼如下:abstract.h
1 #ifndef ABS_H_ 2 #define ABS_H_ 3#include <iostream> 4 #include <string> 5 using namespace std; 6 class Wheels { 7 public: 8 Wheels(); 9 ~Wheels(); 10 virtual string getWheels() = 0; 11 }; 12 13 class BenQWheels :public Wheels { 14 public: 15 BenQWheels(); 16 ~BenQWheels(); 17 stringgetWheels(); 18 }; 19 20 class BMWWheels :public Wheels { 21 public: 22 BMWWheels(); 23 ~BMWWheels(); 24 string getWheels(); 25 }; 26 27 class GeneralWheels :public Wheels { 28 public: 29 GeneralWheels(); 30 ~GeneralWheels(); 31 string getWheels(); 32 }; 33 34class Mirrors { 35 public: 36 Mirrors(); 37 ~Mirrors(); 38 virtual string getMirrors() = 0; 39 }; 40 41 class BMWMirrors :public Mirrors { 42 public: 43 BMWMirrors(); 44 ~BMWMirrors(); 45 string getMirrors(); 46 }; 47 48 class BenQMirrors :public Mirrors { 49 public: 50 BenQMirrors(); 51 ~BenQMirrors(); 52 string getMirrors(); 53 }; 54 55 class GeneralMirrors :public Mirrors { 56 public: 57 GeneralMirrors(); 58 ~GeneralMirrors(); 59 string getMirrors(); 60 61 }; 62 63 class Engine { 64 public: 65 Engine(); 66 ~Engine(); 67 virtual string getEngine() = 0; 68 }; 69 70 class BMWEngine :public Engine { 71 public: 72 BMWEngine(); 73 ~BMWEngine(); 74 string getEngine(); 75 }; 76 77 class BenQEngine :public Engine { 78 public: 79 BenQEngine(); 80 ~BenQEngine(); 81 string getEngine(); 82 }; 83 84 class GeneralEngine :public Engine { 85 public: 86 GeneralEngine(); 87 ~GeneralEngine(); 88 string getEngine(); 89 }; 90 91 class Body 92 { 93 public: 94 Body(); 95 ~Body(); 96 virtual string getBody() = 0; 97 98 }; 99 100 class BenQBody :public Body { 101 public: 102 BenQBody(); 103 ~BenQBody(); 104 string getBody(); 105 }; 106 107 class BMWBody :public Body { 108 public: 109 BMWBody(); 110 ~BMWBody(); 111 string getBody(); 112 }; 113 114 class GeneralBody :public Body { 115 public: 116 GeneralBody(); 117 ~GeneralBody(); 118 string getBody(); 119 }; 120 121 #endif
1 #include "AbstactProduct.h" 2 #include <string> 3 using namespace std; 4 5 Wheels::Wheels() { 6 7 } 8 Wheels::~Wheels() { 9 10 } 11 12 BMWWheels::BMWWheels() { 13 14 } 15 BMWWheels::~BMWWheels() { 16 17 } 18 19 string BMWWheels::getWheels() { 20 return "BMW wheels"; 21 } 22 23 24 BenQWheels::BenQWheels() { 25 26 } 27 BenQWheels::~BenQWheels() { 28 29 } 30 string BenQWheels::getWheels() { 31 return "BenQ wheels"; 32 } 33 34 GeneralWheels::GeneralWheels() { 35 36 } 37 GeneralWheels::~GeneralWheels() { 38 39 } 40 string GeneralWheels::getWheels() { 41 return "General Wheels"; 42 } 43 44 45 Mirrors::Mirrors() { 46 47 } 48 Mirrors::~Mirrors() { 49 50 } 51 52 BenQMirrors::BenQMirrors() { 53 54 } 55 BenQMirrors::~BenQMirrors() { 56 57 } 58 string BenQMirrors::getMirrors() { 59 return "BenQ mirrors"; 60 } 61 62 BMWMirrors::BMWMirrors() { 63 64 } 65 BMWMirrors::~BMWMirrors() { 66 67 } 68 string BMWMirrors::getMirrors() { 69 return "BMW mirrors"; 70 } 71 GeneralMirrors::GeneralMirrors() { 72 73 } 74 GeneralMirrors::~GeneralMirrors() { 75 76 } 77 string GeneralMirrors::getMirrors() { 78 return "General Mirrors"; 79 } 80 81 Engine::Engine() { 82 83 } 84 85 Engine::~Engine() { 86 87 } 88 89 BMWEngine::BMWEngine() { 90 91 } 92 93 BMWEngine::~BMWEngine() { 94 95 } 96 string BMWEngine::getEngine() { 97 return "BMW engine"; 98 } 99 100 BenQEngine::BenQEngine() { 101 102 } 103 104 BenQEngine::~BenQEngine() { 105 106 } 107 108 string BenQEngine::getEngine() { 109 return "BenQ engine"; 110 } 111 112 GeneralEngine::GeneralEngine() { 113 114 } 115 GeneralEngine::~GeneralEngine() { 116 117 } 118 119 string GeneralEngine::getEngine() { 120 return "General engine"; 121 } 122 123 Body::Body() { 124 125 } 126 Body::~Body() { 127 128 } 129 130 BMWBody::BMWBody() { 131 132 } 133 BMWBody::~BMWBody() { 134 135 } 136 137 string BMWBody::getBody() { 138 return "BMW body"; 139 } 140 141 BenQBody::BenQBody() { 142 143 } 144 BenQBody::~BenQBody() { 145 146 } 147 148 string BenQBody::getBody() { 149 return "BenQ body!"; 150 } 151 152 GeneralBody::GeneralBody() { 153 154 } 155 156 GeneralBody::~GeneralBody() { 157 158 } 159 160 string GeneralBody::getBody() { 161 return "General body"; 162 }
Carfactoy.h
#ifndef CAR_FACTORY_H_ #define CAR_FACTORY_H_ #include "AbstactProduct.h" class Car { public: Car(); ~Car(); virtual Wheels* getWheel() = 0; virtual Mirrors* getMirror() = 0; virtual Engine* getEngine() = 0; virtual Body* getBody() = 0; }; class BMWCar :public Car { public: BMWCar(); ~BMWCar(); Wheels* getWheel(); Mirrors* getMirror(); Engine* getEngine(); Body* getBody(); }; class BenQCar :public Car { public: BenQCar(); ~BenQCar(); Wheels* getWheel(); Mirrors* getMirror(); Engine* getEngine(); Body* getBody(); }; class GeneralCar :public Car { public: GeneralCar(); ~GeneralCar(); Wheels* getWheel(); Mirrors* getMirror(); Engine* getEngine(); Body* getBody(); }; #endif // CAR_FACTORY_H_
carfactory.cpp
1 #include "AbstactProduct.h" 2 #include"Carfactory.h" 3 #include<iostream> 4 5 using namespace std; 6 7 Car::Car() { 8 9 } 10 Car::~Car() { 11 12 } 13 14 BMWCar::BMWCar() { 15 16 } 17 BMWCar::~BMWCar() { 18 19 } 20 Wheels* BMWCar::getWheel() { 21 return (new BMWWheels()); 22 } 23 Mirrors* BMWCar::getMirror() { 24 return (new BMWMirrors()); 25 } 26 Engine* BMWCar::getEngine() { 27 return (new BMWEngine()); 28 } 29 Body* BMWCar::getBody() { 30 return (new BMWBody()); 31 } 32 33 BenQCar::BenQCar() { 34 35 } 36 BenQCar::~BenQCar() { 37 38 } 39 Wheels* BenQCar::getWheel() { 40 return (new BenQWheels()); 41 } 42 Mirrors* BenQCar::getMirror() { 43 return (new BenQMirrors()); 44 } 45 Engine* BenQCar::getEngine() { 46 return (new BenQEngine()); 47 } 48 Body* BenQCar::getBody() { 49 return (new BenQBody()); 50 } 51 52 GeneralCar::GeneralCar() { 53 54 } 55 GeneralCar::~GeneralCar() { 56 57 } 58 Wheels* GeneralCar::getWheel() { 59 return (new GeneralWheels()); 60 } 61 Mirrors* GeneralCar::getMirror() { 62 return (new GeneralMirrors()); 63 } 64 Engine* GeneralCar::getEngine() { 65 return (new GeneralEngine()); 66 } 67 Body* GeneralCar::getBody() { 68 return (new GeneralBody()); 69 }
client.cpp
1 //#include "AbstactProduct.h" 2 #include "Carfactory.h" 3 #include<iostream> 4 using namespace std; 5 6 int main(int argc, char* argv[]) 7 { 8 Car* car = new BMWCar(); 9 cout << car->getBody()->getBody() << endl; 10 cout << car->getEngine()->getEngine() << endl; 11 cout << car->getMirror()->getMirrors() << endl; 12 cout << car->getWheel()->getWheels() << endl; 13 system("pause"); 14 }
輸出
創造型設計模式-----抽象工廠模式