|
@@ -0,0 +1,154 @@
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#include<vector>
|
|
|
+#include<iostream>
|
|
|
+#include<string>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+class data
|
|
|
+{
|
|
|
+private:
|
|
|
+ int datas;
|
|
|
+public:
|
|
|
+ data(int n):datas(n){}
|
|
|
+ void print()
|
|
|
+ {
|
|
|
+ std::cout<<datas<<std::endl;
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+class basic_biulder
|
|
|
+{
|
|
|
+private:
|
|
|
+ std::vector<data> elenment;
|
|
|
+public:
|
|
|
+ void builder_data(int n)
|
|
|
+ {
|
|
|
+ elenment.emplace_back(data(n));
|
|
|
+ }
|
|
|
+ void print()
|
|
|
+ {
|
|
|
+ for(auto i:elenment)
|
|
|
+ {
|
|
|
+ i.print();
|
|
|
+ }
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+class builder
|
|
|
+{
|
|
|
+private:
|
|
|
+ std::vector<data> elenment;
|
|
|
+public:
|
|
|
+ builder& builder_data(int n)
|
|
|
+ {
|
|
|
+ elenment.emplace_back(data(n));
|
|
|
+ return *this;
|
|
|
+ }
|
|
|
+ builder& print()
|
|
|
+ {
|
|
|
+ for(auto i:elenment)
|
|
|
+ {
|
|
|
+ i.print();
|
|
|
+ }
|
|
|
+ return *this;
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+class Personbuilder;
|
|
|
+class Personbuilderbase;
|
|
|
+class PersonbuilderAddress;
|
|
|
+
|
|
|
+class person
|
|
|
+{
|
|
|
+ std::string address;
|
|
|
+ std::string name;
|
|
|
+ std::string post_code;
|
|
|
+ std::string city;
|
|
|
+ int age;
|
|
|
+ person(){}
|
|
|
+ friend Personbuilder;
|
|
|
+ friend PersonbuilderAddress;
|
|
|
+public:
|
|
|
+ static Personbuilder create();
|
|
|
+};
|
|
|
+
|
|
|
+class Personbuilderbase
|
|
|
+{
|
|
|
+protected:
|
|
|
+ person& p;
|
|
|
+ explicit Personbuilderbase(person& data):p(data){}
|
|
|
+public:
|
|
|
+ operator person()
|
|
|
+ {
|
|
|
+ std::move(p);
|
|
|
+ }
|
|
|
+ PersonbuilderAddress lives()const;
|
|
|
+
|
|
|
+};
|
|
|
+
|
|
|
+class Personbuilder:public Personbuilderbase
|
|
|
+{
|
|
|
+ person p;
|
|
|
+public:
|
|
|
+ Personbuilder():Personbuilderbase(p){}
|
|
|
+
|
|
|
+};
|
|
|
+
|
|
|
+Personbuilder person::create()
|
|
|
+{
|
|
|
+ return Personbuilder();
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+class PersonbuilderAddress:public Personbuilderbase
|
|
|
+{
|
|
|
+private:
|
|
|
+ using self=PersonbuilderAddress;
|
|
|
+public:
|
|
|
+ explicit PersonbuilderAddress(person& data):Personbuilderbase(data){}
|
|
|
+ self& at(std::string address){
|
|
|
+ this->p.address=std::move(address);
|
|
|
+ return *this;
|
|
|
+ }
|
|
|
+
|
|
|
+};
|
|
|
+
|
|
|
+PersonbuilderAddress Personbuilderbase::lives() const
|
|
|
+{
|
|
|
+ return PersonbuilderAddress(p);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+int main()
|
|
|
+{
|
|
|
+
|
|
|
+ basic_biulder test_1;
|
|
|
+ test_1.builder_data(1);
|
|
|
+ test_1.builder_data(2);
|
|
|
+ test_1.builder_data(3);
|
|
|
+ test_1.builder_data(4);
|
|
|
+ test_1.print();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ builder test_2;
|
|
|
+ test_2.builder_data(1).builder_data(2).builder_data(3).builder_data(4).print();
|
|
|
+
|
|
|
+
|
|
|
+ person data=person::create().lives().at("hello");
|
|
|
+}
|
|
|
+
|