7. Builder mode
7.1 general
Separate the construction and representation of a complex object, so that the same construction process can create different objects.
7.2 structure
Abstract Builder: this interface specifies the creation of those parts of complex objects, and does not involve the creation of specific object parts.
Concrete Builder: implement the builder interface and complete the specific creation method of each part of the complex object. After the construction process is completed, provide an example of the product.
Product class: complex object to be created.
Conductor class: call specific builders to create various components of complex objects. The instructor does not involve the information of specific products, but is only responsible for ensuring that all parts of the object are created completely or in a certain order.
7.2.1 UML structure diagram
7.3 realization
7.3.1 UML diagram
7.3.2 code
#include<iostream> #include<string> using namespace std; //CPU class class Cpu { public: virtual void showCpu() {} }; class IntelCpu : public Cpu { public: void showCpu() { cout << "Intel CPU" << endl; } }; class AmdCpu : public Cpu { public: void showCpu() { cout << "AMD CPU" << endl; } }; //Memory class class Memory { public: virtual void showMemory() {} }; class CorsairMemory : public Memory { public: void showMemory() { cout << "Pirate ship memory module" << endl; } }; class KingstonMemory : public Memory { public: void showMemory() { cout << "Kingston memory module" << endl; } }; //Motherboard class class Mainboard { public: virtual void showMainboard () {} }; class MsiMainboard : public Mainboard { public: void showMainboard() { cout << "MSI motherboard" << endl; } }; class AsusMainboard : public Mainboard { public: void showMainboard() { cout << "Asus motherboard" << endl; } }; //Computer class Computer { private: Cpu* cpu; Memory* memory; Mainboard* mainboard; public: void setCpu(Cpu* cpu) { this->cpu = cpu; } void setMemory(Memory* memory) { this->memory = memory; } void setMainboard(Mainboard* mainboard) { this->mainboard = mainboard; } void getInformation() { this->mainboard->showMainboard(); this->cpu->showCpu(); this->memory->showMemory(); } }; //Builder class class ComputerBuilder { protected: Computer* computer = new Computer(); public: virtual void buildCpu() {} virtual void buildMemory() {} virtual void buildMainboard() {} virtual Computer* getComputer() { return this->computer; } }; class MsiIntelComputerBuilder : public ComputerBuilder { public: void buildCpu() { this->computer->setCpu(new IntelCpu()); } void buildMemory() { this->computer->setMemory(new CorsairMemory()); } void buildMainboard() { this->computer->setMainboard(new MsiMainboard()); } Computer* getComputer() { buildCpu(); buildMemory(); buildMainboard(); return this->computer; } }; class AsusCmdComputerBuilder : public ComputerBuilder { public: void buildCpu() { this->computer->setCpu(new AmdCpu()); } void buildMemory() { this->computer->setMemory(new KingstonMemory()); } void buildMainboard() { this->computer->setMainboard(new AsusMainboard()); } Computer* getComputer() { buildCpu(); buildMemory(); buildMainboard(); return this->computer; } }; //Director class class Director { private: ComputerBuilder* builder; public: Director(ComputerBuilder* builder) { this->builder = builder; } Computer* construct() { return builder->getComputer(); } }; int main() { Director* director = new Director(new MsiIntelComputerBuilder()); Computer* computer = director->construct(); computer->getInformation(); director = new Director(new AsusCmdComputerBuilder()); computer = director->construct(); computer->getInformation(); return 0; }
7.4 advantages and disadvantages
7.4.1 advantages
Good encapsulation. Using the packer mode can effectively encapsulate changes. In the scenario of using builder mode, the general product class and builder class are relatively stable. Therefore, encapsulating the main business logic in the commander class can achieve good stability as a whole.
The client does not need to know the details of the product creation process and the product itself.
The product creation process can be more finely controlled, and the product creation steps can be decomposed into different methods.
It's easy to expand. If there are new requirements, just create a new builder class. There is basically no need to modify the previously tested code.
7.4.2 disadvantages
The products produced by the builder mode generally have more in common and the components are similar. If the product difference is large, it is not suitable to use the builder mode. Therefore, the scope of application has an impact.
7.5 usage scenarios
The creation of objects is complex and consists of multiple components. There are many kinds of components, but the construction method between components is stable.
The algorithm of creating a complex object is independent of the components of the object and their assembly method, that is, the construction process and final representation of the product are independent of each other.
7.6 mode extension
When a class constructor needs to pass in many parameters, if you create an instance of this class, the readability will be poor and it is easy to introduce errors. You can refactor using the builder class.