码迷,mamicode.com
首页 > 编程语言 > 详细

c++ 创建型模式-建造者模式(Builder)

时间:2020-01-22 18:26:27      阅读:57      评论:0      收藏:0      [点我收藏+]

标签:构造   bsp   rect   必须   ali   独立   code   结构   str   

1) 意图:

将一个复杂对象的构建和它的表示分离,使得同样的构建过程可以创建不同的表示

2) 结构:

 技术图片

   

 

 

 

 

 

 

 

 

  其中:

    Builder是提供生成Product各个部件的抽象接口

    ConcreteBuilder 实现Builder的各个接口

    Director 构造一个使用Builder接口的对象

    Product 表示被构造的复杂对象

3) 适用性:

  1. 当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时
  2. 当构造过程必须允许被构造的对象有不同的表示时

4) 举例:

  

 1  #include <iostream>
 2 
 3  class  Product
 4  {
 5  public:
 6      Product() {}
 7      virtual ~Product() {}
 8      void setPartA(std::string param)
 9      {
10          m_partA = param;
11          std::cout << "m_partA = " << m_partA.c_str() << std::endl;
12      }
13      void setPartB(std::string param)
14      {
15          m_partB = param;
16          std::cout << "m_partB = " << m_partB.c_str() << std::endl;
17      }
18      void setPartC(std::string param)
19      {
20          m_partC = param;
21          std::cout << "m_partC = " << m_partC.c_str() << std::endl;
22      }
23  private:
24      std::string m_partA;
25      std::string m_partB;
26      std::string m_partC;
27  };
28  class Builder
29  {
30  public:
31      Builder() {}
32      virtual ~Builder() {}
33      virtual void BuildPartA() = 0;
34      virtual void BuildPartB() = 0;
35      virtual void BuildPartC() = 0;
36  };
37  class ConcreteBuilder : public Builder
38  {
39  public:
40      ConcreteBuilder() 
41      {
42          m_prouduct = new Product();
43      }
44      virtual ~ConcreteBuilder() 
45      {
46          if (m_prouduct)
47          {
48              delete m_prouduct;
49              std::cout << "delete m_prouduct" << std::endl;
50          }
51      }
52      virtual void BuildPartA()
53      {
54          m_prouduct->setPartA("BuidPartA by ConcreteBuilder");
55      }
56      virtual void BuildPartB()
57      {
58          m_prouduct->setPartB("BuidPartB by ConcreteBuilder");
59      }
60      virtual void BuildPartC()
61      {
62          m_prouduct->setPartC("BuidPartC by ConcreteBuilder");
63      }
64  private:
65      Product* m_prouduct;
66  };
67  class  Director
68  {
69  public:
70      Director(Builder* builder):m_builder(builder)
71      {
72      }
73      virtual ~Director() 
74      {
75          if (m_builder)
76          {
77              delete m_builder;
78              std::cout << "delete m_builder" << std::endl;
79         }
80      }
81      void Construct()
82      {
83          m_builder->BuildPartA();
84          m_builder->BuildPartB();
85          m_builder->BuildPartC();
86      }
87  private:
88      Builder* m_builder;
89  };
90  
91  int main()
92  {
93      Builder* builder = new ConcreteBuilder();
94      Director* dirctor = new Director(builder);
95      dirctor->Construct();
96      delete dirctor;
97      system("pause");
98  }

c++ 创建型模式-建造者模式(Builder)

标签:构造   bsp   rect   必须   ali   独立   code   结构   str   

原文地址:https://www.cnblogs.com/ho966/p/12228811.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!