设计模式之建造者模式(Builder Pattern)

1、定义

建造者模式(Builder Pattern)使用多个简单的对象一步一步构建成一个复杂的对象。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

一个 Builder 类会一步一步构造最终的对象。该 Builder 类是独立于其他对象的。

2、介绍

优点: 1、建造者独立,易扩展。 2、便于控制细节风险。

缺点: 1、产品必须有共同点,范围有限制。 2、如内部变化复杂,会有很多的建造类。

使用场景: 1、需要生成的对象具有复杂的内部结构。 2、需要生成的对象内部属性本身相互依赖。

注意事项:与工厂模式的区别是:建造者模式更加关注与零件装配的顺序。

3、源码

类关系图:

具体思路:子类首先调用父类方法。子类重写父类的方法,根据谁调用父类的方法,Run()内就具体调用哪个子类的方法。

运用到知识点:verctor容器及迭代器的运用。 

3.1、头文件

CarModel.h

#pragma once
#include <vector>
#include <iostream>
using namespace std;
class CCarModel
{
public:
	CCarModel(void);
	virtual ~CCarModel(void);
	void Run();
	void SetSequence(vector<string> *pSeq);
protected:
	virtual void Start() = 0;
	virtual void Stop() = 0;
	virtual void Alarm() = 0;
	virtual void EngineBoom() = 0;
private:
	vector<string> * m_pSequence;
};

BenzModel.h

#pragma once
#include "carmodel.h"
class CBenzModel :
    public CCarModel
{
public:
    CBenzModel(void);
    ~CBenzModel(void);
protected:
    void Start();
    void Stop();
    void Alarm();
    void EngineBoom();
};

 BMWModel.h

#pragma once
#include "carmodel.h"
class CBMWModel :
    public CCarModel
{
public:
    CBMWModel(void);
    ~CBMWModel(void);
protected:
    void Start();
    void Stop();
    void Alarm();
    void EngineBoom();
};

ICarBuilder.h

#pragma once
#include "CarModel.h"

class ICarBuilder
{
public:
    ICarBuilder(void)
    {
    }
    virtual ~ICarBuilder(void)
    {
    }
    virtual void SetSequence(vector<string> *pseq) = 0;
    virtual CCarModel * GetCarModel() = 0;
};

BenzBuilder.h

#pragma once
#include "icarbuilder.h"
#include "CarModel.h"
class CBenzBuilder :
    public ICarBuilder
{
public:
    CBenzBuilder(void);
    ~CBenzBuilder(void);
    void SetSequence(vector<string> *pSeq);
    CCarModel * GetCarModel();
private:
    CCarModel *m_pBenz;
};

BMWBuilder.h 

#pragma once
#include "icarbuilder.h"
#include "CarModel.h"
class CBMWBuilder :
    public ICarBuilder
{
public:
    CBMWBuilder(void);
    ~CBMWBuilder(void);
    void SetSequence(vector<string> *pSeq);
    CCarModel * GetCarModel();
private:
    CCarModel *m_pBMW;
};

Director.h

#pragma once
#include "BenzModel.h"
#include "BMWModel.h"
#include "BenzBuilder.h"
#include "BMWBuilder.h"
class CDirector
{
public:
    CDirector(void);
    ~CDirector(void);
    CBenzModel * GetABenzModel();
    CBenzModel * GetBBenzModel();
    CBMWModel * GetCBMWModel();
    CBMWModel * GetDBMWModel();
private:
    vector<string> * m_pSeqence;
    CBenzBuilder * m_pBenzBuilder;
    CBMWBuilder * m_pBMWBuilder;
};

3.2、实现

CarModel.cpp

#include "CarModel.h"
CCarModel::CCarModel(void)
{
}
CCarModel::~CCarModel(void)
{
}
void CCarModel::SetSequence(vector<string> *pSeq)
{
	m_pSequence = pSeq;
}
void CCarModel::Run()
{
	vector<string>::const_iterator it = m_pSequence->begin();
	for (; it < m_pSequence->end(); ++it)
	{
		string actionName = *it;
		if (actionName.compare("start") == 0)
		{
			Start();//哪个子类调用此方法,就指向哪个子类的方法,下同
		}
		else if (actionName.compare("stop") == 0)
		{
			Stop();
		}
		else if (actionName.compare("alarm") == 0)
		{
			Alarm();
		}
		else if (actionName.compare("engine boom") == 0)
		{
			EngineBoom();
		}
	}
}

BenzModel.cpp

#include "BenzModel.h"
CBenzModel::CBenzModel(void)
{
}
CBenzModel::~CBenzModel(void)
{
}
void CBenzModel::Start()
{
    cout << "奔驰发动..." << endl;
}
void CBenzModel::Stop()
{
    cout << "奔驰停车..." << endl;
}
void CBenzModel::Alarm()
{
    cout << "奔驰鸣笛" << endl;
}
void CBenzModel::EngineBoom()
{
    cout << "奔驰引擎声音是这样...." << endl;
}

BMWModel.cpp

#include "BMWModel.h"
CBMWModel::CBMWModel(void)
{
}
CBMWModel::~CBMWModel(void)
{
}
void CBMWModel::Start()
{
    cout << "宝马发动..." << endl;
}
void CBMWModel::Stop()
{
    cout << "宝马停车..." << endl;
}
void CBMWModel::Alarm()
{
    cout << "宝马鸣笛" << endl;
}
void CBMWModel::EngineBoom()
{
    cout << "宝马引擎声音是这样...." << endl;
}

BenzBuilder.cpp

#include "BenzBuilder.h"
#include "BenzModel.h"
CBenzBuilder::CBenzBuilder(void)
{
    m_pBenz = new CBenzModel();
}
CBenzBuilder::~CBenzBuilder(void)
{
    delete m_pBenz;
}
void CBenzBuilder::SetSequence(vector<string> *pSeq)
{
    m_pBenz->SetSequence(pSeq);
}
CCarModel * CBenzBuilder::GetCarModel()
{
    return m_pBenz;
}

BMWBuilder.cpp

#include "BMWBuilder.h"
#include "BMWModel.h"
CBMWBuilder::CBMWBuilder(void)
{
    m_pBMW = new CBMWModel();
}
CBMWBuilder::~CBMWBuilder(void)
{
    delete m_pBMW;
}
void CBMWBuilder::SetSequence( vector<string> *pSeq )
{
    m_pBMW->SetSequence(pSeq);
}
CCarModel * CBMWBuilder::GetCarModel()
{
    return m_pBMW;
}

Director.cpp

#include "Director.h"
CDirector::CDirector(void)
{
    m_pBenzBuilder = new CBenzBuilder();
    m_pBMWBuilder = new CBMWBuilder();
    m_pSeqence = new vector<string>();
}
CDirector::~CDirector(void)
{
    delete m_pBenzBuilder;
    delete m_pBMWBuilder;
    delete m_pSeqence;
}
CBenzModel * CDirector::GetABenzModel()
{
    m_pSeqence->clear();
    m_pSeqence->push_back("start");
    m_pSeqence->push_back("stop");
    m_pBenzBuilder->SetSequence(m_pSeqence);
    return dynamic_cast<CBenzModel*>(m_pBenzBuilder->GetCarModel());
}
CBenzModel * CDirector::GetBBenzModel()
{
    m_pSeqence->clear();
    m_pSeqence->push_back("engine boom");
    m_pSeqence->push_back("start");
    m_pSeqence->push_back("stop");
    m_pBenzBuilder->SetSequence(m_pSeqence);
    return dynamic_cast<CBenzModel*>(m_pBenzBuilder->GetCarModel());
}
CBMWModel * CDirector::GetCBMWModel()
{
    m_pSeqence->clear();
    m_pSeqence->push_back("alarm");
    m_pSeqence->push_back("start");
    m_pSeqence->push_back("stop");
    m_pBMWBuilder->SetSequence(m_pSeqence);
    return static_cast<CBMWModel*>(m_pBMWBuilder->GetCarModel());
}
CBMWModel * CDirector::GetDBMWModel()
{
    m_pSeqence->clear();
    m_pSeqence->push_back("start");
    m_pBenzBuilder->SetSequence(m_pSeqence);
    return dynamic_cast<CBMWModel*>(m_pBMWBuilder->GetCarModel());
}

Builder.cpp

#include "CarModel.h"
#include "BenzModel.h"
#include "BMWModel.h"
#include "BenzBuilder.h"
#include "BMWBuilder.h"
#include "Director.h"
#include <vector>
#include <iostream>
using std::vector;
using std::string;
using std::cout;
using std::endl;


void DoBenzRun()  //没有使用模式时,需要把步骤一条一条的传入模型。
{
    cout << "----------生成奔驰模型----------" << endl;
    CBenzModel *pBenz = new CBenzModel();
    vector<string> seq;
    seq.push_back("engine boom");//客户要求run的时候先发动引擎
    seq.push_back("start");//启动起来
    seq.push_back("stop");//开了一段就停下来

    pBenz->SetSequence(&seq);
    pBenz->Run();
    delete pBenz;
}

//使用模式后,由benzBuilder和bmwBuilder来生成,并且使用同样的创建顺序。
void DoBuilder()    
{
    cout << "----------用同一个顺序,生成模型----------" << endl;
    vector<string> seq;
    seq.push_back("engine boom");
    seq.push_back("start");
    seq.push_back("stop");

    CBenzBuilder benzBuilder;
    benzBuilder.SetSequence(&seq);
    CBenzModel *pBenz = dynamic_cast<CBenzModel*>(benzBuilder.GetCarModel());
    pBenz->Run();

    CBMWBuilder bmwBuilder;
    bmwBuilder.SetSequence(&seq);
    CBMWModel *pBmw = dynamic_cast<CBMWModel*>(bmwBuilder.GetCarModel());
    pBmw->Run();
}
void DoDirector()    //使用指导者来封装创建的逻辑,把创建的顺序内聚在指导者类里面。
{
    cout << "----------批量生成模型----------" << endl;
    CDirector director;

    //1W辆A类型的奔驰车
    for(int i = 0; i < 2; i++)
        director.GetABenzModel()->Run();

    //100W辆B类型的奔驰车
    for(int i = 0; i < 2; i++)
        director.GetBBenzModel()->Run();

    //1000W辆C类型的宝马车
    for(int i = 0; i < 2; i++)
        director.GetCBMWModel()->Run();
}
int main()
{
    DoBenzRun();

    DoBuilder();

    DoDirector();

    system("pause");
    return 0;
}

4、结果

参考文献:《菜鸟教程》   https://blog.csdn.net/phiall/article/details/52199659博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值