其他分享
首页 > 其他分享> > Builder-建造者模式

Builder-建造者模式

作者:互联网

设计模式之建造者模式


Builder模式要解决的问题:当我们创建对象是一个很复杂的问题(通常是由很多其他的对象组合而成的),我们要这个复杂的对象一步一步创建,在创建时根据实际情况创建对象。使得经过相同的步骤创建的对象,所得到的对象展示也不一样。
UML图

Product 类

/*
 * @Descripttion: 
 * @version: 
 * @Author: aaron
 * @Date: 2021-11-21 19:30:38
 * @LastEditors: aaron
 * @LastEditTime: 2021-11-26 17:06:56
 */
#ifndef _PRODUCT_H_
#define _PRODUCT_H_
#include<iostream>
using namespace std;
class Product
{
private:
public:
    Product();
    ~Product();

    string ProductA();
};

#endif // _PRODUCT_H_



/*
 * @Descripttion: 
 * @version: 
 * @Author: aaron	
 * @Date: 2021-11-21 19:32:48
 * @LastEditors: aaron
 * @LastEditTime: 2021-11-21 19:34:18
 */
#include"Product.h"
#include<iostream>
using namespace std;
Product::Product()
{
cout<<"I am Product"<<endl;
}

Product::~Product(){

}

Builder 类 ,重要

/*
 * @Descripttion:  Builder 设计模式 builder 
 * @version: 
 * @Author: aaron
 * @Date: 2021-11-26 17:03:31
 * @LastEditors: aaron
 * @LastEditTime: 2021-12-01 13:32:54
 */

#ifndef _BUILDER_H_
#define _BUILDER_H_

#include "Product.h"

class Builder
{
protected:
    Builder();

private:
public:
    virtual ~Builder();

    virtual void BuilderPartA(string builder) = 0;
    virtual void BuilderPartB(string builder) = 0;
    virtual void BuilderPartC(string builder) = 0;
    virtual Product *GetProduct() = 0;
};

#endif
/*
 * @Descripttion: 
 * @version: 
 * @Author: aaron
 * @Date: 2021-11-26 17:24:30
 * @LastEditors: aaron
 * @LastEditTime: 2021-11-30 21:34:02
 */
#ifndef _CONCRETEBUILDER_H_
#define _CONCRETEBUILDER_H_

#include "Builder.h"

class ConcreteBuilder : public Builder
{
private:
public:
    ConcreteBuilder();
    ~ConcreteBuilder();

    void BuilderPartB(string builder);
    void BuilderPartC(string builder);
    void BuilderPartA(string builder);

    Product *GetProduct();
};

#endif // _CONCRETEBUILDER_H_

/*
 * @Descripttion: 
 * @version: 
 * @Author: aaron
 * @Date: 2021-11-26 17:34:46
 * @LastEditors: aaron
 * @LastEditTime: 2021-11-30 21:31:29
 */

#include "ConcreteBuilder.h"

void ConcreteBuilder::BuilderPartB(string builder)
{
    cout << "ConcreteBuilder::BuilderPartB" << endl;
}
void ConcreteBuilder::BuilderPartC(string builder)
{
    cout << "ConcreteBuilder::BuilderPartC" << endl;
}
void ConcreteBuilder::BuilderPartA(string builder)
{
    cout << "ConcreteBuilder::BuilderPartA" << endl;
}

/**
 * @name: 产品 
 * @test: 
 * @msg: 
 * @param {*}
 * @return {*}
 */
Product *ConcreteBuilder::GetProduct()
{

    string name = "1";
    BuilderPartB(name);
    BuilderPartA(name);
    BuilderPartC(name);

    Product *p = new Product();
    return p;
}

ConcreteBuilder::~ConcreteBuilder()
{
    cout << "ConcreteBuilder 析构函数" << endl;
}

/**
 * @name: 构造函数
 * @test: 
 * @msg: 
 * @param {*}
 * @return {*}
 */
ConcreteBuilder::ConcreteBuilder()
{
    cout << "ConcreteBuilder 构造函数" << endl;
}

Director 类,它不创建对象,它扮演的角色很重要

/*
 * @Descripttion: 导演类
 * @version: 
 * @Author: aaron
 * @Date: 2021-11-30 20:09:05
 * @LastEditors: aaron
 * @LastEditTime: 2021-11-30 21:20:43
 */

#ifndef _DIRECTOR_H_
#define _DIRECTOR_H_
#include "Builder.h"
class Director
{
private:
public:
    // 导演类
    Director(Builder *bld);

    ~Director();

public:
    void Construct();

private:
    Builder *m_bld;
};

#endif // _DIRECTOR_H_
/*
 * @Author: aaron
 * @Date: 2021-11-30 20:12:57
 * @LastEditors: aaron
 * @LastEditTime: 2021-11-30 21:29:51
 * @Description: file content
 * @FilePath: /Learn/设计模式/Builder/src/Director.cpp
 */
/*
 * @Descripttion: 
 * @version: 
 * @Author: 秦武胜
 * @Date: 2021-11-30 20:12:57
 * @LastEditors: 秦武胜
 * @LastEditTime: 2021-11-30 20:12:58
 */

#include "Director.h"

Director::Director(Builder *bld)
{
    m_bld = bld;
}

Director::~Director()
{
    cout << "Director 析构函数" << endl;
}

/**
 * @description: 
 * @param {*}
 * @return {*}
 */
void Director::Construct()
{
    string name = "1";
    m_bld->BuilderPartA(name);
    name = "2";
    m_bld->BuilderPartB(name);
    name = "3";
    m_bld->BuilderPartC(name);
}
/*
 * @Descripttion: 
 * @version: 
 * @Author: aaron
 * @Date: 2021-11-21 17:33:56
 * @LastEditors: aaron
 * @LastEditTime: 2021-11-30 21:25:55
 */
#include <iostream>
#include "Product.h"
#include "Builder.h"
#include "ConcreteBuilder.h"
#include "Director.h"
int main()
{

    // Product p;
    // std::cout<<"hello world"<<std::endl;
    Director *bld = new Director(new ConcreteBuilder());
    bld->Construct();

    return 0;
}

github链接

标签:11,Product,Builder,建造,模式,aaron,2021,include
来源: https://blog.csdn.net/weixin_43098502/article/details/121653388