别院牧志知识库 别院牧志知识库
首页
  • 基础

    • 全栈之路
    • 😎Awesome资源
  • 进阶

    • Python 工匠系列
    • 高阶知识点
  • 指南教程

    • Socket 编程
    • 异步编程
    • PEP 系列
  • 面试

    • Python 面试题
    • 2025 面试记录
    • 2022 面试记录
    • 2021 面试记录
    • 2020 面试记录
    • 2019 面试记录
    • 数据库索引原理
  • 基金

    • 基金知识
    • 基金经理
  • 细读经典

    • 德隆-三个知道
    • 孔曼子-摊大饼理论
    • 配置者说-躺赢之路
    • 资水-建立自己的投资体系
    • 反脆弱
  • Git 参考手册
  • 提问的智慧
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
首页
  • 基础

    • 全栈之路
    • 😎Awesome资源
  • 进阶

    • Python 工匠系列
    • 高阶知识点
  • 指南教程

    • Socket 编程
    • 异步编程
    • PEP 系列
  • 面试

    • Python 面试题
    • 2025 面试记录
    • 2022 面试记录
    • 2021 面试记录
    • 2020 面试记录
    • 2019 面试记录
    • 数据库索引原理
  • 基金

    • 基金知识
    • 基金经理
  • 细读经典

    • 德隆-三个知道
    • 孔曼子-摊大饼理论
    • 配置者说-躺赢之路
    • 资水-建立自己的投资体系
    • 反脆弱
  • Git 参考手册
  • 提问的智慧
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 辨析

  • Sockets编程

  • Django

  • stackoverflow

  • Flask

  • 全栈之路

  • 面试

  • 代码片段

  • 异步编程

  • 😎Awesome资源

  • PEP

  • Python工匠系列

  • 高阶知识点

  • Python 学习资源待整理
  • 设计模式

    • Python 设计模式
    • 装饰器模式
    • 抽象工厂模式
    • 建造者模式/生成器模式
    • 原型模式
    • 单例模式
    • 设计模式(2)——工厂方法模式
    • 设计模式(3)——抽象工厂模式
    • 设计模式(4)——模板方法模式
    • 设计模式(5)——代理模式
    • 设计模式(6)——建造者模式
      • 基本思想和原则
      • 动机
      • 实现
      • 优点
    • 设计模式(7)——策略模式
    • 设计模式(8)——命令模式
    • 设计模式(9)——原型模式
    • 设计模式(10)——中介者模式
    • 设计模式(11)——责任链模式
    • 设计模式(12)——装饰器模式
    • 设计模式(13)——适配器模式
    • 设计模式(14)——迭代器模式
    • 设计模式(15)——观察者模式
    • 设计模式(16)——外观模式
    • 设计模式(17)——状态模式
    • 设计模式(18)——桥接模式
    • 设计模式(19)——享元模式
    • 设计模式(20)——解释器模式
    • 设计模式(21)——组合模式
    • 设计模式(23)——备忘录模式
    • Python 全栈之路系列之单例设计模式
    • 设计模式(22)——访问者模式
    • 工厂方法模式
    • Python 设计模式资源收集
  • 好“艹蛋”的 Python 呀!
  • FIFO | 待学清单📝
  • pip 安装及使用
  • 数据分析

  • 源码阅读计划

  • OOP

  • 关于 python 中的 setup.py
  • 并行分布式框架 Celery
  • 七种武器,让你的代码提高可维护性
  • 使用 pdb 调试 Python 代码
  • 每周一个 Python 标准库
  • 🐍Python
  • 设计模式
佚名
2017-12-08
目录

设计模式(6)——建造者模式

本文介绍建造者模式的概念和应用。

# 基本思想和原则

将一个复杂对象的构建和它的表示分离,使同样的构建过程可以创建不同的表示。

使用 Builder 类封装类实例的创建过程,客户代码使用各个 Builder 来构建对象而不是直接使用 new 创建类实例。

# 动机

当一个类中某个方法中的内部调用顺序不同,会产生不同的结果时,可以考虑使用建造者模式。将产生特定调用顺序的类实例用 Builder 进行封装,供客户代码使用可以简化整个对象创建过程。当需要产生新的调用顺序地类实例时,只需要创建对应的 Builder 即可。

# 实现

public abstract class Robot {
    private ArrayList<String> actionSequence = new ArrayList<String>();

    protected abstract void speak();

    protected abstract void walk();

    protected abstract void think();

    public final void demo() {
        for (int i = 0; i < this.actionSequence.size(); i++) {
            String action = this.actionSequence.get(i);
            if (action.equalsIgnoreCase("speak")) {
                this.speak();
            } else if (action.equalsIgnoreCase("walk")) {
                this.walk();
            } else if (action.equalsIgnoreCase("think")) {
                this.think();
            }
        }
    }

    public final void setActionSequence(ArrayList actionSequence) {
        this.actionSequence = actionSequence;
    }
}

public class RobotA extends Robot {
    @Override
    protected void speak() {
        System.out.println("RobotA speak...");
    }

    @Override
    protected void walk() {
        System.out.println("RobotA walk...");
    }

    @Override
    protected void think() {
        System.out.println("RobotA think...");
    }
}

public class RobotB extends Robot {
    @Override
    protected void speak() {
        System.out.println("RobotB speak...");
    }

    @Override
    protected void walk() {
        System.out.println("RobotB walk...");
    }

    @Override
    protected void think() {
        System.out.println("RobotB think...");
    }
}

public abstract class RobotBuilder {
    public abstract void setActionSequence(ArrayList<String> actionSequence);

    public abstract Robot getRobot();
}

public class RobotABuilder extends RobotBuilder {
    private RobotA robotA = new RobotA();

    public void setActionSequence(ArrayList<String> actionSequence) {
        this.robotA.setActionSequence(actionSequence);
    }

    public Robot getRobot() {
        return this.robotA;
    }
}

public class RobotBBuilder extends RobotBuilder {
    private RobotB robotB = new RobotB();

    public void setActionSequence(ArrayList<String> actionSequence) {
        this.robotB.setActionSequence(actionSequence);
    }

    public Robot getRobot() {
        return this.robotB;
    }
}

public class Test {
    public static void main(String[] args) {
        ArrayList<String> actionSequence1 = new ArrayList<String>();
        actionSequence1.add("speak");
        actionSequence1.add("think");
        actionSequence1.add("walk");

        ArrayList<String> actionSequence2 = new ArrayList<String>();
        actionSequence1.add("think");
        actionSequence1.add("walk");
        actionSequence1.add("speak");

        RobotBuilder robotABuilder = new RobotABuilder();
        RobotBuilder robotBBuilder = new RobotBBuilder();

        robotABuilder.setActionSequence(actionSequence1);
        robotABuilder.getRobot().demo();

        System.out.println("\n");

        robotABuilder.setActionSequence(actionSequence2);
        robotABuilder.getRobot().demo();

        System.out.println("\n");

        robotBBuilder.setActionSequence(actionSequence1);
        robotBBuilder.getRobot().demo();

        System.out.println("\n");

        robotBBuilder.setActionSequence(actionSequence2);
        robotBBuilder.getRobot().demo();

    }
}
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

输出:

RobotA speak...
RobotA think...
RobotA walk...

RobotA think...
RobotA walk...
RobotA speak...

RobotB speak...
RobotB think...
RobotB walk...

RobotB think...
RobotB walk...
RobotB speak...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

上面的代码中,机器人可以有说话、走路和思考三种动作,demo方法用来执行机器人演示,我们想随意组合这些动作为一个动作序列来演示,。因此动作序列是可配置的。由于动作序列可以多种多样,使用建造者模式可以将各个动作序列封装在各个 Builder 中,客户代码直接调用 Builder 即可创建具有特定动作序列的机器人。

# 优点

建造者模式对一个类产生具体实例做了相应的封装,使客户代码不需要了解具体类的内部细节,可以直接使用相应的 Builder 来创建实例。各个 Builder 之间具有很好的隔离性,都可以独立做改变而不会互相影响。

编辑 (opens new window)
#设计模式
上次更新: 2024-07-23, 01:00:43
设计模式(5)——代理模式
设计模式(7)——策略模式

← 设计模式(5)——代理模式 设计模式(7)——策略模式→

最近更新
01
2025 面试记录
05-28
02
提升沟通亲和力的实用策略
03-26
03
工作
07-15
更多文章>
Theme by Vdoing | Copyright © 2019-2025 IMOYAO | 别院牧志
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式