#include #include using namespace std; class CPU { public: string type; CPU(string s) { type = s; } }; class Memory { public: string type; Memory(string s) { type = s; } }; class Computer { unique_ptr cpu; unique_ptr mem; friend class Builder; public: void show() { cout << "CPU: " << cpu->type << endl << "Memory: " << mem->type << endl; } }; class Builder { public: unique_ptr c; Builder() { c = make_unique(); } // Method chaining Builder& set_cpu(string s) { c->cpu = make_unique(s); return *this; } Builder& set_mem(string s) { c->mem = make_unique(s); return *this; } unique_ptr build() { // You can check if the computer is valid here. return move(c); } }; int main() { Builder b; // You can pack `b.set().set()...build()` into a Director. auto c = b .set_cpu("Intel") .set_mem("Samsung") .build(); c->show(); }