In software engineering, Design Pattern is a solution to various common (recurring) problems in software design. According to the purpose of the pattern, GoF (Gang of Four) Design Pattern can be divided into the following three types:
1. Creation mode: used to describe "how to create objects". Its main feature is "separating the creation and use of objects". Including singleton, prototype, factory method, abstract factory and builder.
2. Structural mode: it is used to describe how to form a larger structure of classes or objects according to a certain layout. It includes 7 modes: agent, adapter, overseas connection, decoration, appearance, element sharing and combination.
3. Behavioral pattern: used to identify common communication patterns between objects and how to allocate responsibilities. It includes 11 modes: template method, policy, command, responsibility chain, state, observer, mediator, iterator, visitor, memo and interpreter.
Today we introduce a common design pattern.
1, Builder mode
Builder Pattern decomposes a complex object into several relatively simple parts, then creates them according to different needs, and finally constructs the complex object.
Builder pattern: separate the construction of complex objects from their representation, so that the same construction process can produce different representations.
The builder mode can be considered in the following cases:
Object creation is complex, but the sub object creation algorithm of each part is similar.
The requirements change greatly, and the sub objects of complex objects often change, but the algorithm of combining them is relatively stable.
advantage:
Separating the creation and presentation of objects, the client does not need to know the specific construction details.
When adding a new product object, you only need to add a specific construction class without modifying the original code, which is convenient for expansion.
The builder mode is not recommended when there are large differences between products, large internal changes and complexity.
/* Key code: builder class: create and provide instances; Director class: manages the dependencies of built instances. */ #include <iostream> #include <string> using namespace std; // Specific product categories class Order { private: string m_strFood; string m_strDrink; public: void setFood(const string& food) { m_strFood = food; } const string& food() { cout << m_strFood.data() << endl; return m_strFood; } void setDrink(const string& drink) { m_strDrink = drink; } const string& drink() { cout << m_strDrink << endl; return m_strDrink; } } //Abstract construction class to provide construction interface. class OrderBuilder { public: virtual ~OrderBuilder() { cout << "~OrderBuilder()" << endl; } virtual void setOrderFood() = 0; virtual void setOrderDrink() = 0; virtual Order* getOrder() = 0; } //Specific construction category class VegetarianOrderBuilder : public OrderBuilder { public: VegetarianOrderBuilder() { m_pOrder = new Order; } ~VegetarianOrderBuilder() { cout << "~VegetarianOrderBuilder()" << endl; delete m_pOrder; m_pOrder = nullptr; } void setOrderFood() override { m_pOrder->setFood("vegetable salad"); } void setOrderDrink() override { m_pOrder->setDrink("warter"); } Order* getOrder() override { return m_pOrder; } private: Order* m_pOrder; }; //Specific construction category class MeatOrderBuilder : public OrderBuilder { public: MeatOrderBuilder() { m_pOrder = new Order; } ~MeatOrderBuilder() { cout << "~MeatOrderBuilder()" << endl; delete m_pOrder; m_pOrder = nullptr; } void setOrderFood() override { m_pOrder->setFood("beef"); } void setOrderDrink() override { m_pOrder-setDrink("beer"); } Order* getorder() override { return m_pOrder; } private: Order* m_pOrder; }; // The Director class is responsible for managing the dependencies of instance creation and directing the builder class to create instances class Director { public: Director(OrderBuilder *builder) : m_pOrderBuilder(builder) { } void construct() { m_pOrderBuilder->setOrderFood(); m_pOrderBuilder->setOrderDrink(); } private: OrderBuilder* m_pOrderBuilder; } int main() { // MeatOrderBuilder* mBuilder = new MeatOrderBuilder; OrderBuilder* mBuilder = new MeatOrderBuilder; // Note that abstract class construction must have a virtual destructor Director* director = new Director(mBuilder); director->construct(); Order* order = mBuilder->getOrder(); order->food(); order->drink(); delete director; director = nullptr; delete mBuilder; mBuilder = nullptr; return 0; }