4 创建型模式-----建造者模式

模式动机:一个对象由若干部件组成,而组合这些部件的过程比较复杂,因此可以把这个组合工作交给建造者来完成,建造这返回给客户的是一个组装好的对象。

模式定义(Builder Pattern)将一个复杂对象的构建与表示分离开来,使得同样的构建过程可以创建不同的表示形式。

 

模式结构图:

 

其中包含抽象建造者Builder、具体建造者ConcreteBuilder、指挥者Director、产品Product。该模式中引入了Director,负责控制产品的生产及其与用户的隔离工作。Director针对抽象建造者Builder编程,客户只要知道具体建造者ConcreteBuilder的类型就能得到一个完整的产品。

 

模式代码:

bt_建造者模式.h

 1 #ifndef BP_H
 2 #define BP_H
 3 
 4 #include <iostream>
 5 
 6 using namespace std;
 7 
 8 /*
 9     产品类
10 */
11 class Product
12 {
13 public:
14     const string& getA() const{ return partA; }
15     void setA(const string& a){ partA = a; }
16     const string& getB() const{ return partB; }
17     void setB(const string& b){ partB = b; }
18     const string& getC() const{ return partC; }
19     void setC(const string& c){ partC = c; }
20 
21 private:
22     string partA;
23     string partB;
24     string partC;
25 };
26 
27 /*
28     抽象建造者类
29 */
30 class Builder
31 {
32 public:
33     virtual ~Builder(){ };
34     virtual void buildPartA() = 0;
35     virtual void buildPartB() = 0;
36     virtual void buildPartC() = 0;
37     virtual Product* getResult() = 0;
38 };
39 
40 /*
41     具体建造者类
42 */
43 class ConcreteBuilder : public Builder
44 {
45 public:
46     ConcreteBuilder()
47     {
48         product = new Product;      // 具体工厂new一个Product对象,这个工作也可以在抽象工厂中完成
49     }
50 
51     virtual void buildPartA()
52     {
53         product->setA("AAA");
54         cout << "构建完成A部分" << endl;
55     }
56     virtual void buildPartB()
57     {
58         product->setB("BBB");
59         cout << "构建完成B部分" << endl;
60     }
61     virtual void buildPartC()
62     {
63         product->setC("CCC");
64         cout << "构建完成C部分" << endl;
65     }
66     virtual Product* getResult()
67     {
68         cout << "构建工作全部完成了" << endl;
69         return product;
70     }
71 
72 private:
73     Product* product;
74 };
75 
76 /*
77     指挥者类
78 */
79 class Director
80 {
81 public:
82     Director(Builder* builder)
83     {
84         this->builder = builder;
85     }
86 
87     Product* construct()
88     {
89         builder->buildPartA();
90         builder->buildPartB();
91         builder->buildPartC();
92         return builder->getResult();
93     }
94 
95 private:
96     Builder* builder;
97 };
98 
99 #endif // BP_H

 

bt_建造者模式.cpp

 1 #include "bt_建造者模式.h"
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
    cout << "***** 建造者模式测试 *****" << endl;
8 Builder* builder = new ConcreteBuilder; 9 Director* director = new Director(builder); 10 Product* product = director->construct(); 11 12 delete product; 13 delete director; 14 delete builder; 15 16 return 0; 17 }
 
 

 

模式优缺点:使用不同的具体建造者,用户可以得到不同的具体对象;增加新的建造者无需修改抽象类即可更改建造方式,因此,对于产品内部组成部分不变的情况下,可以很容易实现花样建造,符合“开闭原则”; 但是如果产品内部发生变化,那么就必须修改建造者。

 

 

 

转载于:https://www.cnblogs.com/benxintuzi/p/4538391.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值