建造者模式:
结构:
1.产品对象:一类产品的抽象,具体产品的建造应该在具体建造者中实现
3.抽象建造者
4.具体建造者:对于每种具体产品都应该有一个对应的建造者,其中应该有一个抽象产品对象成员变量
5.装配者:建造者对各个零件单独建造,没有顺序,因此,需要早装配者中确定建造顺序
优点:
1.建造者独立,易扩展
缺点:
1.产品必须具有相同属性
2.产品越多,需要的建造者类也就越多
场景:
手机有很多,小米,华为,三星,苹果,虽然名称不一样,但是内部结构基本差不多。这种场景就很适合使用建造者模式:
1 //IBuilder.h 2 #pragma once 3 #include "Phone.h" 4 class IBuilder 5 { 6 public: 7 IBuilder(); 8 ~IBuilder(); 9 protected: 10 Phone* phone_; 11 public: 12 virtual void build_camera() = 0; 13 virtual void build_earphone() = 0; 14 virtual void build_screen() = 0; 15 virtual Phone* get_phone(); 16 }; 17 18 class XiaomiBuilder:public IBuilder 19 { 20 public: 21 XiaomiBuilder(); 22 void build_camera(); 23 void build_earphone(); 24 void build_screen(); 25 }; 26 27 28 class HuaweiBuilder :public IBuilder 29 { 30 public: 31 HuaweiBuilder(); 32 void build_camera(); 33 void build_earphone(); 34 void build_screen(); 35 }; 36 37 //IBuilder.cpp 38 #include "pch.h" 39 #include "IBuilder.h" 40 41 42 IBuilder::IBuilder() 43 { 44 } 45 46 47 IBuilder::~IBuilder() 48 { 49 } 50 51 Phone* IBuilder::get_phone() 52 { 53 return phone_; 54 } 55 56 XiaomiBuilder::XiaomiBuilder() 57 { 58 phone_ = new Phone; 59 } 60 61 void XiaomiBuilder::build_camera() 62 { 63 phone_->set_camera("xiaomi camera!!1"); 64 } 65 66 void XiaomiBuilder::build_screen() 67 { 68 phone_->set_screen("xiaomi screen!!!"); 69 } 70 71 void XiaomiBuilder::build_earphone() 72 { 73 phone_->set_earphone("xiaomi earphone!!!"); 74 } 75 76 HuaweiBuilder::HuaweiBuilder() 77 { 78 phone_ = new Phone; 79 } 80 81 void HuaweiBuilder::build_camera() 82 { 83 phone_->set_camera("huawei camera!!1"); 84 } 85 86 void HuaweiBuilder::build_screen() 87 { 88 phone_->set_screen("huawei screen!!!"); 89 } 90 91 void HuaweiBuilder::build_earphone() 92 { 93 phone_->set_earphone("huawei earphone!!!"); 94 } 95 96 //Assembler.h 97 #pragma once 98 #include "IBuilder.h" 99 class Assembler 100 { 101 public: 102 Assembler(); 103 ~Assembler(); 104 void create(IBuilder* builedr); 105 }; 106 107 //Assembler.cpp 108 #include "pch.h" 109 #include "Assembler.h" 110 111 112 Assembler::Assembler() 113 { 114 } 115 116 117 Assembler::~Assembler() 118 { 119 } 120 121 122 void Assembler::create(IBuilder* builedr) 123 { 124 builedr->build_screen(); 125 builedr->build_earphone(); 126 builedr->build_camera(); 127 } 128 129 //Phone.h 130 #pragma once 131 #include <iostream> 132 #include <string.h> 133 using namespace std; 134 135 class Phone 136 { 137 public: 138 Phone(); 139 ~Phone(); 140 void set_camera(string camera); 141 void set_screen(string screen); 142 void set_earphone(string earphone); 143 void display_camera(); 144 void display_earphone(); 145 void display_screen(); 146 private: 147 string camera_; 148 string screen_; 149 string earphone_; 150 }; 151 152 //Phone.cpp 153 #include "pch.h" 154 #include "Phone.h" 155 156 157 Phone::Phone() 158 { 159 } 160 161 162 Phone::~Phone() 163 { 164 } 165 166 void Phone::set_camera(string camera) 167 { 168 camera_ = camera; 169 } 170 171 void Phone::set_earphone(string earphone) 172 { 173 earphone_ = earphone; 174 } 175 176 void Phone::set_screen(string screen) 177 { 178 screen_ = screen; 179 } 180 181 void Phone::display_camera() 182 { 183 cout << camera_.c_str() << endl; 184 } 185 186 void Phone::display_earphone() 187 { 188 cout << earphone_.c_str() << endl; 189 } 190 191 void Phone::display_screen() 192 { 193 cout << screen_.c_str() << endl; 194 } 195 196 //main.cpp 197 #include "Phone.h" 198 #include "IBuilder.h" 199 using namespace std; 200 201 #ifndef SAFE_DELETE 202 #define SAFE_DELETE(p){if(p){delete (p);(p)=NULL;}} 203 #endif 204 205 206 int main() 207 { 208 IBuilder* xiaomi = new XiaomiBuilder; 209 IBuilder* huawei = new HuaweiBuilder; 210 Assembler* assembler = new Assembler; 211 assembler->create(xiaomi); 212 assembler->create(huawei); 213 214 Phone* xiaomi_phone = xiaomi->get_phone(); 215 Phone* huawei_phone = huawei->get_phone(); 216 217 xiaomi_phone->display_camera(); 218 xiaomi_phone->display_earphone(); 219 xiaomi_phone->display_screen(); 220 221 huawei_phone->display_camera(); 222 huawei_phone->display_earphone(); 223 huawei_phone->display_screen(); 224 225 226 SAFE_DELETE(xiaomi); 227 SAFE_DELETE(xiaomi_phone); 228 SAFE_DELETE(huawei); 229 SAFE_DELETE(huawei_phone); 230 SAFE_DELETE(assembler); 231 232 getchar(); 233 }
//运行结果