工程源码:
设计模式-创建型模式-建造者模式
https://download.csdn.net/download/qq_40788199/85547856
码云:
设计模式-创建型模式-建造者模式https://gitee.com/gongguixing/c-design-mode.git
1、模式的定义与特点
建造者(Builder)模式的定义:指将一个复杂对象的构造与它的表示分离,使同样的构建过程可以创建不同的表示,这样的设计模式被称为建造者模式。它是将一个复杂的对象分解为多个简单的对象,然后一步一步构建而成。它将变与不变相分离,即产品的组成部分是不变的,但每一部分是可以灵活选择的。
该模式的主要优点如下:
- 封装性好,构建和表示分离。
- 扩展性好,各个具体的建造者相互独立,有利于系统的解耦。
- 客户端不必知道产品内部组成的细节,建造者可以对创建过程逐步细化,而不对其它模块产生任何影响,便于控制细节风险。
其缺点如下:
- 产品的组成部分必须相同,这限制了其使用范围。
- 如果产品的内部变化复杂,如果产品内部发生变化,则建造者也要同步修改,后期维护成本较大。
建造者(Builder)模式和工厂模式的关注点不同:建造者模式注重零部件的组装过程,而工厂方法模式更注重零部件的创建过程,但两者可以结合使用。
2、模式的结构与实现
建造者(Builder)模式由产品、抽象建造者、具体建造者、指挥者等 4 个要素构成。
1. 模式的结构
建造者(Builder)模式的主要角色如下。
- 产品角色(Product):它是包含多个组成部件的复杂对象,由具体建造者来创建其各个零部件。
- 抽象建造者(Builder):它是一个包含创建产品各个子部件的抽象方法的接口,通常还包含一个返回复杂产品的方法 getResult()。
- 具体建造者(Concrete Builder):实现 Builder 接口,完成复杂产品的各个部件的具体创建方法。
- 指挥者(Director):它调用建造者对象中的部件构造与装配方法完成复杂对象的创建,在指挥者中不涉及具体产品的信息。
2. 模式的实现
(1) 产品角色:包含多个组成部件的复杂对象。
(2) 抽象建造者:包含创建产品各个子部件的抽象方法。
(3) 具体建造者:实现了抽象建造者接口。
(4) 指挥者:调用建造者中的方法完成复杂对象的创建。
3、代码实现:
抽象建造者:装修工人
#ifndef ABSTRACTDECORATOR_H
#define ABSTRACTDECORATOR_H
#include "parlour.h"
//抽象建造者:装修工人
class AbstractDecorator
{
public:
AbstractDecorator();
virtual ~AbstractDecorator() {}
virtual void buildWall() = 0;
virtual void buildTV() = 0;
virtual void buildSofa() = 0;
Parlour * getResult();
protected:
Parlour *m_pParlour;
};
#endif // ABSTRACTDECORATOR_H
#include "abstractdecorator.h"
AbstractDecorator::AbstractDecorator()
{
m_pParlour = new Parlour();
}
Parlour *AbstractDecorator::getResult()
{
return m_pParlour;
}
指挥者:项目经理
#ifndef PROJECTMANAGER_H
#define PROJECTMANAGER_H
#include "abstractdecorator.h"
//指挥者:项目经理
class ProjectManager
{
public:
ProjectManager(AbstractDecorator *p);
//产品构建与组装方法
Parlour *decorate();
private:
AbstractDecorator * m_pAD;
};
#endif // PROJECTMANAGER_H
#include "projectmanager.h"
ProjectManager::ProjectManager(AbstractDecorator *p)
{
m_pAD = p;
}
Parlour * ProjectManager::decorate()
{
m_pAD->buildWall();
m_pAD->buildTV();
m_pAD->buildSofa();
return m_pAD->getResult();
}
产品:客厅
#ifndef PARLOUR_H
#define PARLOUR_H
#include "string"
using namespace std;
//产品:客厅
class Parlour
{
public:
Parlour();
void setWall(const string &wall);
void setTV(const string &TV);
void setSofa(const string &sofa);
void show();
private:
string m_wall; //墙
string m_TV; //电视
string m_sofa; //沙发
};
#endif // PARLOUR_H
#include "parlour.h"
#include "iostream"
Parlour::Parlour()
{
}
void Parlour::setWall(const string &wall)
{
m_wall = wall;
}
void Parlour::setTV(const string &TV)
{
m_TV = TV;
}
void Parlour::setSofa(const string &sofa)
{
m_sofa = sofa;
}
void Parlour::show()
{
std::cout << "My Parlour" << std::endl;
std::cout << m_wall << std::endl;
std::cout << m_TV << std::endl;
std::cout << m_TV << std::endl;
}
调用例子
#include <iostream>
#include "gddecorator.h"
#include "gxdecorator.h"
#include "projectmanager.h"
#include "parlour.h"
int main()
{
// 具体建造者:具体装修工人GD ,builder
AbstractDecorator *pABGD = new GDDecorator();
// 指挥者:项目经理
ProjectManager *pPMGD = new ProjectManager(pABGD);
// 产品构建与组装
Parlour * pGD = pPMGD->decorate();
// 展示
pGD->show();
std::cout << "-------------------" << std::endl;
// 具体建造者:具体装修工人GX ,builder
AbstractDecorator *pABGX = new GXDecorator();
// 指挥者:项目经理
ProjectManager *pPMGX = new ProjectManager(pABGX);
// 产品构建与组装
Parlour * pGX = pPMGX->decorate();
// 展示
pGX->show();
// 记得释放对象 delete
return 0;
}
建造者具体实现在工程文件