builder.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //构造器模式
  2. //构造器严格意义上来说不能算一个单独的设计模式,更多的是对各种组件的构造方式的封装
  3. #include<vector>
  4. #include<iostream>
  5. #include<string>
  6. //将一个类的构造函数进行封装,使得只能用特定的API才能构造对象(和后面的工厂有一些交叉的地方)
  7. class data //一个最简单的组件
  8. {
  9. private:
  10. int datas;
  11. public:
  12. data(int n):datas(n){}
  13. void print()
  14. {
  15. std::cout<<datas<<std::endl;
  16. }
  17. };
  18. //最简单的构造器(对对应对象的构造函数的封装)
  19. class basic_biulder
  20. {
  21. private:
  22. std::vector<data> elenment;
  23. public:
  24. void builder_data(int n)
  25. {
  26. elenment.emplace_back(data(n));
  27. }
  28. void print()
  29. {
  30. for(auto i:elenment)
  31. {
  32. i.print();
  33. }
  34. }
  35. };
  36. //流式构造器
  37. class builder
  38. {
  39. private:
  40. std::vector<data> elenment;
  41. public:
  42. builder& builder_data(int n)
  43. {
  44. elenment.emplace_back(data(n));
  45. return *this;
  46. }
  47. builder& print()
  48. {
  49. for(auto i:elenment)
  50. {
  51. i.print();
  52. }
  53. return *this;
  54. }
  55. };
  56. //组合构造器
  57. //当然能不这么做尽量不要,结构乱七八糟的
  58. class Personbuilder;
  59. class Personbuilderbase;
  60. class PersonbuilderAddress;
  61. class person //被构造的数据
  62. {
  63. std::string address;
  64. std::string name;
  65. std::string post_code;
  66. std::string city;
  67. int age;
  68. person(){}
  69. friend Personbuilder; //只有设置为友元才能访问构造函数
  70. friend PersonbuilderAddress;
  71. public:
  72. static Personbuilder create();
  73. };
  74. class Personbuilderbase
  75. {
  76. protected:
  77. person& p;
  78. explicit Personbuilderbase(person& data):p(data){}
  79. public:
  80. operator person() //类型转换运算符
  81. {
  82. return std::move(p);
  83. }
  84. PersonbuilderAddress lives()const;
  85. };
  86. class Personbuilder:public Personbuilderbase
  87. {
  88. person p;
  89. public:
  90. Personbuilder():Personbuilderbase(p){}
  91. };
  92. Personbuilder person::create()
  93. {
  94. return Personbuilder();
  95. }
  96. class PersonbuilderAddress:public Personbuilderbase
  97. {
  98. private:
  99. using self=PersonbuilderAddress;
  100. public:
  101. explicit PersonbuilderAddress(person& data):Personbuilderbase(data){}
  102. self& at(std::string address){
  103. this->p.address=std::move(address);
  104. return *this;
  105. }
  106. //其余的赋值函数可以写在下面
  107. };
  108. PersonbuilderAddress Personbuilderbase::lives() const
  109. {
  110. return PersonbuilderAddress(p);
  111. }
  112. int main()
  113. {
  114. //简单构造器
  115. basic_biulder test_1;
  116. test_1.builder_data(1);
  117. test_1.builder_data(2);
  118. test_1.builder_data(3);
  119. test_1.builder_data(4);
  120. test_1.print();
  121. //流式构造器
  122. builder test_2;
  123. test_2.builder_data(1).builder_data(2).builder_data(3).builder_data(4).print(); //能通过流式方法调用
  124. //组合构造器
  125. person data=person::create().lives().at("hello");
  126. }