Builder.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include <stdio.h>
  2. /*
  3. * C++ Design Patterns: BUILDER
  4. * http://www.plantuml.com/plantuml/uml/hL9HJiCm3FtFAIpniS5KAdwdgXhjG492Q1fmWBJre2XjAiT1JC21UevUZ2pB1c9eN-GdplFpUqxYcM59R1ibuX5aZpUtywibZ6BXLgcaCJ2l2KlM1Ey2t8gWiBMgaFnf0gLk3PCjUNmQ48ESRIdZiEyo2zALOy8JSWXTqQSGTSj8AraYX8BWTRBrMaZYIyVy1MM7KExrGkEvRaj2ng1vNPFX83mv4Fu9POTGleTSxqiqLl4HgtjqhrBzkvVOVyLP1C5AX1sIZoZwyyh76A5J8LhD2DB0tOMtJeeqARNID2MzbYfJ5jDGaSG-aIPnaUwu3-iEfni0SaB9jMwbyha5KeCCIe8P-awIpq651fgXvsVj8i-hy7kEDem8R0kwU45sUDCfV4FFwz0oo5TT4rJrQZNqX2qxDSAEXjwuWtSmvmBT-syk9MOkSjFz1G00
  5. *
  6. */
  7. #include <iostream>
  8. #include <string>
  9. class Product {
  10. public:
  11. void partA(const std::string &part) {
  12. part_a = part;
  13. }
  14. void partB(const std::string &part) {
  15. part_b = part;
  16. }
  17. void partC(const std::string &part) {
  18. part_c = part;
  19. }
  20. void checkProduct() {
  21. std::cout << part_a + " " + part_b + " " + part_c + " have been created" << std::endl;
  22. }
  23. private:
  24. std::string part_a;
  25. std::string part_b;
  26. std::string part_c;
  27. };
  28. class Builder {
  29. public:
  30. Builder() { std::cout << "Constructor Builder" << std::endl; }
  31. virtual ~Builder() {}
  32. Product getProduct() { return product; }
  33. virtual void buildPartA() =0;
  34. virtual void buildPartB() =0;
  35. virtual void buildPartC() =0;
  36. protected:
  37. Product product;
  38. };
  39. class ConcreateBuilder1 : public Builder {
  40. public:
  41. ConcreateBuilder1() { std::cout << "Constructor ConcreateBuilder-1" << std::endl; }
  42. ~ConcreateBuilder1() {}
  43. void buildPartA() {
  44. product.partA("1-A");
  45. }
  46. void buildPartB() {
  47. product.partB("1-B");
  48. }
  49. void buildPartC() {
  50. product.partC("1-C");
  51. }
  52. };
  53. class ConcreateBuilder2 : public Builder {
  54. public:
  55. ConcreateBuilder2() { std::cout << "Constructor ConcreateBuilder-2" << std::endl; }
  56. ~ConcreateBuilder2() {}
  57. void buildPartA() {
  58. product.partA("2-A");
  59. }
  60. void buildPartB() {
  61. product.partB("2-B");
  62. }
  63. void buildPartC() {
  64. product.partC("2-C");
  65. }
  66. };
  67. class Director {
  68. public:
  69. Director() { std::cout << "Constructor Director" << std::endl; }
  70. ~Director() {
  71. if (builder) {
  72. delete builder;
  73. }
  74. }
  75. void setBuilder(Builder *b) {
  76. if (builder) {
  77. delete builder;
  78. }
  79. builder = b;
  80. }
  81. Product getProduct() {
  82. Builder *builder_ok = dynamic_cast<Builder*>(builder);
  83. if (builder_ok) {
  84. return builder->getProduct();
  85. } else {
  86. std::cout << "Director does construct : empty_product !" << std::endl;
  87. Product empty_product;
  88. return empty_product;}
  89. }
  90. void construct() {
  91. Builder* builder_ok = dynamic_cast<Builder*>(builder);
  92. std::cout << "Director does construct" << std::endl;
  93. if (builder_ok) {
  94. builder_ok->buildPartA();
  95. builder_ok->buildPartB();
  96. builder_ok->buildPartC();
  97. } else {
  98. std::cout << "Director does construct : no method available !" << std::endl;
  99. }
  100. }
  101. private:
  102. Builder *builder = nullptr;
  103. };
  104. int main(int argc, char* argv[]) {
  105. Director director;
  106. director.setBuilder(new ConcreateBuilder1);
  107. director.construct();
  108. Product product1 = director.getProduct();
  109. product1.checkProduct();
  110. director.setBuilder(new ConcreateBuilder2);
  111. director.construct();
  112. Product product2 = director.getProduct();
  113. product2.checkProduct();
  114. director.setBuilder(0);
  115. director.construct();
  116. Product product3 = director.getProduct();
  117. product3.checkProduct();
  118. }