听峰问雨 听峰问雨
首页
导航站
  • 编程语言

    • Python
  • 数据结构与算法
  • 设计模式
  • UVA
  • LeetCode
  • 《Go语言实战》
  • 《Go Web编程》
  • 《算法精粹 经典计算机科学问题的Python实现》
  • 学习
  • 博客搭建
  • 本站

    • 分类
    • 标签
    • 归档
  • 我的

    • 收藏
    • 关于
GitHub (opens new window)

zfprotectors

默默学习er
首页
导航站
  • 编程语言

    • Python
  • 数据结构与算法
  • 设计模式
  • UVA
  • LeetCode
  • 《Go语言实战》
  • 《Go Web编程》
  • 《算法精粹 经典计算机科学问题的Python实现》
  • 学习
  • 博客搭建
  • 本站

    • 分类
    • 标签
    • 归档
  • 我的

    • 收藏
    • 关于
GitHub (opens new window)
  • Python

  • Go

  • 数据结构与算法

  • 设计模式

    • 【设计模式】1 引言
    • 创建型

      • 【设计模式】创建型模式-Abstract Factory模式
      • 【设计模式】创建型模式-Builder模式
      • 【设计模式】创建型模式-Factory模式
      • 【设计模式】创建型模式-Prototype模式
      • 【设计模式】创建型模式-Sington模式
    • 结构型

    • 行为型

  • 程序设计层
  • 设计模式
  • 创建型
zfprotectors
2022-05-18

【设计模式】创建型模式-Builder模式

建造者模式使用多个简单的对象一步一步构建成复杂的对象

# 功能

将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示

# 解决

  • 主要解决:在软件系统中,有时候面临着"一个复杂对象"的创建工作,其通常由各个部分的子对象用一定的算法构成;由于需求的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将它们组合在一起的算法却相对稳定。
  • 何时使用:一些基本部件不会变,而其组合经常变化的时候。
  • 如何解决:将变与不变分离开。
  • 关键代码:建造者:创建和提供实例,导演:管理建造出来的实例的依赖关系。

# 与抽象工厂模式区别

Builder模式强调的是一步步创建对象,并通过相同的创建过程可以获得不同的结果对象,一般对象不是直接返回的。 AbstractFactory模式强调的是为创建多个相互依赖的对象提供一个同一的接口,对象是直接返回的

# 优缺点

  • 优点:
    • 建造者独立,易扩展
    • 便于控制细节风险
  • 缺点:
    • 产品必须有共同点,范围有限制
    • 如果内部变化复杂,会有很多的建造类

# 应用场景

  • 应用实例:去吃KFC,其中汉堡,可乐,薯条做法均是不会变的,但是可以组合成不同的套餐
  • 使用场景:需要生成的对象具有复杂的内部结构和内部属性本身相互依赖

# 简单示例代码部分

Builder.hpp


//Builder.hpp
#ifndef _BUILDER_H_
#define _BUILDER_H_
#include <string>
#include <iostream>
using namespace std;
class Product{
public:
    Product(){}
    ~Product(){}
    void ProductPart(){
        cout<<"build part a product..."<<endl;
    }
};
class ProductPart{
public:
    ProductPart(){}
    ~ProductPart(){}
    ProductPart* BuildPart(){
        return new ProductPart;
    }
};
class Builder{
public:
    Builder(){}
    ~Builder(){}
    virtual void BuildPartA(const string& buildPara)=0;
    virtual void BuildPartB(const string& buildPara)=0;
    virtual void BuildPartC(const string& buildPara)=0;
    virtual Product* GetProduct()=0;
};
class ConcreteBuilder:public Builder{
public:
    ConcreteBuilder(){}
    ~ConcreteBuilder(){}
    void BuildPartA(const string& buildPara){
        cout<<"Step1:Build PartA.."<<buildPara<<endl;
    }
    void BuildPartB(const string& buildPara){
        cout<<"Step1:Build PartB.."<<buildPara<<endl;
    }
    void BuildPartC(const string& buildPara){
        cout<<"Step1:Build PartC.."<<buildPara<<endl;
    }
    Product* GetProduct(){
        BuildPartA("pre-defined");
        BuildPartB("pre-defined");
        BuildPartC("pre-defined");
        return new Product();
    }
};
class Director{
public:
    Director(Builder* bld){ _bld=bld; }
    ~Director(){}
    void Construct(){
        _bld->BuildPartA("user-defined");
        _bld->BuildPartB("user-defined");
        _bld->BuildPartC("user-defined");
    }
private:
    Builder* _bld;
};
#endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

main.cpp

#include "Builder.hpp"
#include <iostream>
using namespace std;
int main(){
    Director* d = new Director(new ConcreteBuilder());
    d->Construct();
    return 0;
}
1
2
3
4
5
6
7
8
编辑 (opens new window)
#设计模式
上次更新: 2022/05/18, 15:47:37
【设计模式】创建型模式-Abstract Factory模式
【设计模式】创建型模式-Factory模式

← 【设计模式】创建型模式-Abstract Factory模式 【设计模式】创建型模式-Factory模式→

最近更新
01
LeetCode88 - 合并两个有序数组
06-22
02
LeetCode1 - 两数之和
06-22
03
LeetCode1603 - 设计停车系统
06-21
更多文章>
Theme by Vdoing | Copyright © 2021-2022 zfprotectors | 闽ICP备2021014222号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式