标签:构造 bsp rect 必须 ali 独立 code 结构 str
1) 意图:
将一个复杂对象的构建和它的表示分离,使得同样的构建过程可以创建不同的表示
2) 结构:
其中:
Builder是提供生成Product各个部件的抽象接口
ConcreteBuilder 实现Builder的各个接口
Director 构造一个使用Builder接口的对象
Product 表示被构造的复杂对象
3) 适用性:
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 }
标签:构造 bsp rect 必须 ali 独立 code 结构 str
原文地址:https://www.cnblogs.com/ho966/p/12228811.html