当前位置:网站首页>建造者模式/生成器模式
建造者模式/生成器模式
2022-08-03 15:53:00 【l_ethan】
定义
建造者模式是一种创建型设计模式,能够分步骤创建复杂对象。该模式允许使用相同的创建代码生题不同类型和形式的对象。
建造者模式建议将对象构造代码从产品类中抽取出来,并将其放在一个名为生成器的独立对象中。
结构

- 生成器(Builder)接口声明在所有类型生成器中通用的产品构造步骤。
- 具体生成器(Concrete Builders)提供构造过程的不同实现。具体生成器也可以构造不遵循通用接口的产品。
- 产品(Products)是最终生成的对象。由不同生成器构造的产品无需属于同一类层次结构或接口。
- 主管(Director)类定义调用构造步骤的顺序,这样你就可以创建和复用特定的产品配置。
- 客户端(Client)必须将某个生成器对象与主管类关联。一般情况下,你只需通过主管类构造函数的参数进行一次性关联即可。此后主管类就能使用生成器对象完成后续所有的构造任务。但在客户端将生成器对象传递给主管类制造方法时还有另一种方式。在这种情况下,你在使用主管类生产产品时每次都可以使用不同的生成器。
优点
可以分步创建对象,暂缓创建步骤或递归运行创建步骤
生成不同形式的产品时,你可以复用相同的制造代码
单一职责原则。你可以将复杂构造代码从产品的业务逻辑中分离出来
缺点
由于该模式需要新增多个类,因此代码整体复杂度增加
案例
#include <iostream>
#include <string>
#include <mutex>
using namespace std;
class Car {
public:
Car(){}
void set_car_tire(string t) {
tire = t;
cout << "set tire:" << tire << endl;
}
void set_car_steering_wheel(string sw) {
steering_wheel = sw;
cout << "set steering wheel:" << steering_wheel << endl;
}
void set_car_engine(string e) {
engine = e;
cout << "set engine:" << engine << endl;
}
private:
string tire;//轮胎
string steering_wheel;//方向盘
string engine;//发动机
};
//抽象建造者
class CarBuilder {
public:
Car getCar() {
return car;
}
virtual void buildTire() = 0;
virtual void buildSteeringWheel() = 0;
virtual void buildEngine() = 0;
protected:
Car car;
};
//具体建造者 奔驰
class BenzBuilder :public CarBuilder {
public:
//具体实现方法
virtual void buildTire() {
car.set_car_tire("benz_tire");
}
virtual void buildSteeringWheel() {
car.set_car_steering_wheel("benz_steering_wheel");
}
virtual void buildEngine() {
car.set_car_engine("benz_engine");
}
};
//具体建造者 奔驰
class AudiBuilder :public CarBuilder {
public:
//具体实现方法
virtual void buildTire() {
car.set_car_tire("audi_tire");
}
virtual void buildSteeringWheel() {
car.set_car_steering_wheel("audi_steering_wheel");
}
virtual void buildEngine() {
car.set_car_engine("audi_engine");
}
};
//指挥者
class Director {
public:
Director() : builder(nullptr) {
}
void set_builder(CarBuilder* cb) {
builder = cb;
}
//组装汽车
Car constructcar() {
builder->buildTire();
builder->buildSteeringWheel();
builder->buildEngine();
return builder->getCar();
}
private:
CarBuilder* builder;
};
int main()
{
CarBuilder* builder;
Director* director = new Director();
Car car;
builder = new BenzBuilder();
director->set_builder(builder);
car = director->constructcar();
delete builder;
builder = new AudiBuilder();
director->set_builder(builder);
car = director->constructcar();
delete builder;
delete director;
return 0;
}边栏推荐
- 5v充8.4v1A电流充电管理ic
- Reptile attention
- Interpretation of the 2021 Cost of Data Breach Report
- 瞌睡检测系统介绍
- 技术干货|如何将 Pulsar 数据快速且无缝接入 Apache Doris
- Small Tools (4) integrated Seata1.5.2 distributed transactions
- 【Unity入门计划】基本概念(6)-精灵渲染器 Sprite Renderer
- NodeJs - cross domain
- Awesome!Coroutines are finally here!Thread is about to be in the past
- Some optional strategies and usage scenarios for PWA application Service Worker caching
猜你喜欢

JS基础--判断

一个文件管理系统的软硬件配置清单

Js array method is summarized
![[Code Hoof Set Novice Village 600 Questions] Define a function as a macro](/img/7c/7e1469090ca3d1dea703b3fcee7428.png)
[Code Hoof Set Novice Village 600 Questions] Define a function as a macro

leetcode:899. 有序队列【思维题】

Introduction to the advantages of the new generation mesh network protocol T-Mesh wireless communication technology

扫雷?拿来吧你(递归展开+坐标标记)

Neural networks, cool?

【899. 有序队列】

参与便有奖,《新程序员》杂志福利来袭!
随机推荐
posgresql 到 es 报这个错误 ,啥意思
Introduction to spark learning - 1
语音识别新一轮竞争打响,自然对话会是下一个制高点吗?
【QT】Qt项目demo:数据在ui界面上显示,鼠标双击可弹窗显示具体信息
CopyOnWriteArrayList详解
Reptile attention
Spark entry learning-2
我在滴滴做开源
A new round of competition for speech recognition has started. Will natural dialogue be the next commanding height?
Three key expectations for the crypto market in August Price moves north?Still expected to be in turmoil
泰山OFFICE技术讲座:段落边框的绘制难点在哪里?
Leetcode76. Minimal Covering Substring
【Unity入门计划】基本概念(7)-Input Manager&Input类
扩展欧几里得求逆元实例
Ruoyi Ruoyi framework @DataScope annotation use and some problems encountered
聊聊这个SaaS领域爆火的话题
PWA 应用 Service Worker 缓存的一些可选策略和使用场景
After the cnpm installation is successful, the prompt is not an internal and external command, nor is it a runnable command solution
自定SvgIcon公用组件
How to play deep paging with hundreds of millions of data?Compatible with MySQL + ES + MongoDB