今儿直白的用盖房子为例,给你讲讲Java建造者模式
建造者模式(Builder Pattern)又叫生成器模式,是一种对象构建模式。它可以将复杂对象的建造过程抽象出来(抽象类别),使这个抽象过程的不同实现方法可以构造出不同表现(属性)的对象。
本文分享自华为云社区《【Java设计模式】用盖房子案例讲解建造者模式(生成器模式)》,作者: 我是一棵卷心菜 。
现在我们需要建房子,过程为打桩、砌墙、封顶。房子有各种各样的,比如普通房,高楼,别墅,各种房子的过程虽然一样,但是要求不要相同的。请编写程序,完成需求
一、传统方式
1、类图分析
2、代码分析
先创建一个抽象类AbstractHouse
,具有打地基、砌墙、封装屋顶以及建房子的功能
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-java"><span style="color:#cc99cd">public</span> <span style="color:#cc99cd">abstract</span> <span style="color:#cc99cd">class</span> <span style="color:#f8c555">AbstractHouse</span> <span style="color:#cccccc">{</span>
<span style="color:#999999">//打地基</span>
<span style="color:#cc99cd">public</span> <span style="color:#cc99cd">abstract</span> <span style="color:#cc99cd">void</span> <span style="color:#f08d49">buildBasic</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
<span style="color:#999999">//砌墙</span>
<span style="color:#cc99cd">public</span> <span style="color:#cc99cd">abstract</span> <span style="color:#cc99cd">void</span> <span style="color:#f08d49">buildWalls</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
<span style="color:#999999">//封装屋顶</span>
<span style="color:#cc99cd">public</span> <span style="color:#cc99cd">abstract</span> <span style="color:#cc99cd">void</span> <span style="color:#f08d49">roofed</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
<span style="color:#999999">//开始建房子</span>
<span style="color:#cc99cd">public</span> <span style="color:#cc99cd">void</span> <span style="color:#f08d49">build</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span> <span style="color:#cccccc">{</span>
<span style="color:#f08d49">buildBasic</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
<span style="color:#f08d49">buildWalls</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
<span style="color:#f08d49">roofed</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
<span style="color:#cccccc">}</span>
<span style="color:#cccccc">}</span>
</code></span></span>
然后选择自己想建造的房子,我选择的是普通的房子,对应类CommonHouse
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-java"><span style="color:#cc99cd">public</span> <span style="color:#cc99cd">class</span> <span style="color:#f8c555">CommonHouse</span> <span style="color:#cc99cd">extends</span> <span style="color:#f8c555">AbstractHouse</span> <span style="color:#cccccc">{</span>
<span style="color:#cccccc">@Override</span>
<span style="color:#cc99cd">public</span> <span style="color:#cc99cd">void</span> <span style="color:#f08d49">buildBasic</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span> <span style="color:#cccccc">{</span>
<span style="color:#f8c555">System</span><span style="color:#cccccc">.</span>out<span style="color:#cccccc">.</span><span style="color:#f08d49">println</span><span style="color:#cccccc">(</span><span style="color:#7ec699">"普通房子开始打地基"</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
<span style="color:#cccccc">}</span>
<span style="color:#cccccc">@Override</span>
<span style="color:#cc99cd">public</span> <span style="color:#cc99cd">void</span> <span style="color:#f08d49">buildWalls</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span> <span style="color:#cccccc">{</span>
<span style="color:#f8c555">System</span><span style="color:#cccccc">.</span>out<span style="color:#cccccc">.</span><span style="color:#f08d49">println</span><span style="color:#cccccc">(</span><span style="color:#7ec699">"普通房子开始建墙了"</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
<span style="color:#cccccc">}</span>
<span style="color:#cccccc">@Override</span>
<span style="color:#cc99cd">public</span> <span style="color:#cc99cd">void</span> <span style="color:#f08d49">roofed</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span> <span style="color:#cccccc">{</span>
<span style="color:#f8c555">System</span><span style="color:#cccccc">.</span>out<span style="color:#cccccc">.</span><span style="color:#f08d49">println</span><span style="color:#cccccc">(</span><span style="color:#7ec699">"普通房子准备封顶喽"</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
<span style="color:#cccccc">}</span>
<span style="color:#cccccc">}</span>
</code></span></span>
最后写一个类Client
用来检测结果
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-java"><span style="color:#cc99cd">public</span> <span style="color:#cc99cd">class</span> <span style="color:#f8c555">Client</span> <span style="color:#cccccc">{</span>
<span style="color:#cc99cd">public</span> <span style="color:#cc99cd">static</span> <span style="color:#cc99cd">void</span> <span style="color:#f08d49">main</span><span style="color:#cccccc">(</span><span style="color:#f8c555">String</span><span style="color:#cccccc">[</span><span style="color:#cccccc">]</span> args<span style="color:#cccccc">)</span> <span style="color:#cccccc">{</span>
<span style="color:#f8c555">CommonHouse</span> commonHouse <span style="color:#67cdcc">=</span> <span style="color:#cc99cd">new</span> <span style="color:#f8c555">CommonHouse</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
commonHouse<span style="color:#cccccc">.</span><span style="color:#f08d49">build</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
<span style="color:#cccccc">}</span>
<span style="color:#cccccc">}</span>
</code></span></span>
3、代码结果
4、问题分析
优点是比较好理解,简单易操作。
缺点是设计的程序结构过于简单,没有设计缓存层对象,程序的扩展和维护不好。也就是说,这种设计方案,把产品(即房子)和创建产品的过程(即建房子流程) 封装在一起,耦合性增强了。
二、建造者模式
1、基本介绍
建造者模式(Builder Pattern)又叫生成器模式,是一种对象构建模式。它可以将复杂对象的建造过程抽象出来(抽象类别),使这个抽象过程的不同实现方法可以构造出不同表现(属性)的对象。
建造者模式是一步一步创建一个复杂的对象,它允许用户只通过指定复杂对象的类型和内容就可以构建它们,用户不需要知道内部的具体构建细节。
2、四个角色
- Product(产品角色): 一个具体的产品对象。
- Builder(抽象建造者): 创建一个Product对象的各个部件指定的 接口/抽象类。
- ConcreteBuilder(具体建造者): 实现接口,构建和装配各个部件。
- Director(指挥者): 构建一个使用Builder接口的对象。它主要是用于创建一个复杂的对象。它主要有两个作用,一是:隔离了客户与对象的生产过程,二是:负责控制产品对象的生产过程
3、类图分析
4、代码分析
先创建一个房子类House
,具有打地基、砌墙和封装屋顶的功能
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-java"><span style="color:#cc99cd">public</span> <span style="color:#cc99cd">class</span> <span style="color:#f8c555">House</span> <span style="color:#cccccc">{</span>
<span style="color:#cc99cd">private</span> <span style="color:#f8c555">String</span> baise<span style="color:#cccccc">;</span>
<span style="color:#cc99cd">private</span> <span style="color:#f8c555">String</span> wall<span style="color:#cccccc">;</span>
<span style="color:#cc99cd">private</span> <span style="color:#f8c555">String</span> roofed<span style="color:#cccccc">;</span>
<span style="color:#cc99cd">public</span> <span style="color:#f8c555">String</span> <span style="color:#f08d49">getBaise</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span> <span style="color:#cccccc">{</span>
<span style="color:#cc99cd">return</span> baise<span style="color:#cccccc">;</span>
<span style="color:#cccccc">}</span>
<span style="color:#cc99cd">public</span> <span style="color:#cc99cd">void</span> <span style="color:#f08d49">setBaise</span><span style="color:#cccccc">(</span><span style="color:#f8c555">String</span> baise<span style="color:#cccccc">)</span> <span style="color:#cccccc">{</span>
<span style="color:#cc99cd">this</span><span style="color:#cccccc">.</span>baise <span style="color:#67cdcc">=</span> baise<span style="color:#cccccc">;</span>
<span style="color:#cccccc">}</span>
<span style="color:#cc99cd">public</span> <span style="color:#f8c555">String</span> <span style="color:#f08d49">getWall</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span> <span style="color:#cccccc">{</span>
<span style="color:#cc99cd">return</span> wall<span style="color:#cccccc">;</span>
<span style="color:#cccccc">}</span>
<span style="color:#cc99cd">public</span> <span style="color:#cc99cd">void</span> <span style="color:#f08d49">setWall</span><span style="color:#cccccc">(</span><span style="color:#f8c555">String</span> wall<span style="color:#cccccc">)</span> <span style="color:#cccccc">{</span>
<span style="color:#cc99cd">this</span><span style="color:#cccccc">.</span>wall <span style="color:#67cdcc">=</span> wall<span style="color:#cccccc">;</span>
<span style="color:#cccccc">}</span>
<span style="color:#cc99cd">public</span> <span style="color:#f8c555">String</span> <span style="color:#f08d49">getRoofed</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span> <span style="color:#cccccc">{</span>
<span style="color:#cc99cd">return</span> roofed<span style="color:#cccccc">;</span>
<span style="color:#cccccc">}</span>
<span style="color:#cc99cd">public</span> <span style="color:#cc99cd">void</span> <span style="color:#f08d49">setRoofed</span><span style="color:#cccccc">(</span><span style="color:#f8c555">String</span> roofed<span style="color:#cccccc">)</span> <span style="color:#cccccc">{</span>
<span style="color:#cc99cd">this</span><span style="color:#cccccc">.</span>roofed <span style="color:#67cdcc">=</span> roofed<span style="color:#cccccc">;</span>
<span style="color:#cccccc">}</span>
<span style="color:#cccccc">}</span>
</code></span></span>
接着创建抽象的建造者HouseBuilder
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-java"><span style="color:#cc99cd">public</span> <span style="color:#cc99cd">abstract</span> <span style="color:#cc99cd">class</span> <span style="color:#f8c555">HouseBuilder</span> <span style="color:#cccccc">{</span>
<span style="color:#cc99cd">public</span> <span style="color:#f8c555">House</span> house <span style="color:#67cdcc">=</span> <span style="color:#cc99cd">new</span> <span style="color:#f8c555">House</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
<span style="color:#999999">//将建造的流程写好,抽象的方法</span>
<span style="color:#cc99cd">public</span> <span style="color:#cc99cd">abstract</span> <span style="color:#cc99cd">void</span> <span style="color:#f08d49">buildBasic</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
<span style="color:#cc99cd">public</span> <span style="color:#cc99cd">abstract</span> <span style="color:#cc99cd">void</span> <span style="color:#f08d49">buildWalls</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
<span style="color:#cc99cd">public</span> <span style="color:#cc99cd">abstract</span> <span style="color:#cc99cd">void</span> <span style="color:#f08d49">roofed</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
<span style="color:#999999">//建造房子好后,将产品(房子)返回</span>
<span style="color:#cc99cd">public</span> <span style="color:#f8c555">House</span> <span style="color:#f08d49">buildHouse</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span> <span style="color:#cccccc">{</span>
<span style="color:#cc99cd">return</span> house<span style="color:#cccccc">;</span>
<span style="color:#cccccc">}</span>
<span style="color:#cccccc">}</span>
</code></span></span>
然后创建指挥者HouseDirector
,动态的指定制作流程,返回产品
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-java"><span style="color:#cc99cd">public</span> <span style="color:#cc99cd">class</span> <span style="color:#f8c555">HouseDirector</span> <span style="color:#cccccc">{</span>
<span style="color:#f8c555">HouseBuilder</span> houseBuilder <span style="color:#67cdcc">=</span> <span style="color:#cc99cd">null</span><span style="color:#cccccc">;</span>
<span style="color:#999999">//空参构造器</span>
<span style="color:#cc99cd">public</span> <span style="color:#f8c555">HouseDirector</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span> <span style="color:#cccccc">{</span>
<span style="color:#cccccc">}</span>
<span style="color:#999999">//通过setter传入houseBuilder</span>
<span style="color:#cc99cd">public</span> <span style="color:#cc99cd">void</span> <span style="color:#f08d49">setHouseBuilder</span><span style="color:#cccccc">(</span><span style="color:#f8c555">HouseBuilder</span> houseBuilder<span style="color:#cccccc">)</span> <span style="color:#cccccc">{</span>
<span style="color:#cc99cd">this</span><span style="color:#cccccc">.</span>houseBuilder <span style="color:#67cdcc">=</span> houseBuilder<span style="color:#cccccc">;</span>
<span style="color:#cccccc">}</span>
<span style="color:#999999">//如何处理建造房子的流程,交给指挥者</span>
<span style="color:#cc99cd">public</span> <span style="color:#f8c555">House</span> <span style="color:#f08d49">constructorHouse</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span> <span style="color:#cccccc">{</span>
houseBuilder<span style="color:#cccccc">.</span><span style="color:#f08d49">buildBasic</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
houseBuilder<span style="color:#cccccc">.</span><span style="color:#f08d49">buildWalls</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
houseBuilder<span style="color:#cccccc">.</span><span style="color:#f08d49">roofed</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
<span style="color:#cc99cd">return</span> houseBuilder<span style="color:#cccccc">.</span><span style="color:#f08d49">buildHouse</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
<span style="color:#cccccc">}</span>
<span style="color:#cccccc">}</span>
</code></span></span>
刚才建造的是普通的房子,现在我想要建造一个高楼类HighBuilding
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-java"><span style="color:#cc99cd">public</span> <span style="color:#cc99cd">class</span> <span style="color:#f8c555">HighBuilding</span> <span style="color:#cc99cd">extends</span> <span style="color:#f8c555">HouseBuilder</span> <span style="color:#cccccc">{</span>
<span style="color:#cccccc">@Override</span>
<span style="color:#cc99cd">public</span> <span style="color:#cc99cd">void</span> <span style="color:#f08d49">buildBasic</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span> <span style="color:#cccccc">{</span>
<span style="color:#f8c555">System</span><span style="color:#cccccc">.</span>out<span style="color:#cccccc">.</span><span style="color:#f08d49">println</span><span style="color:#cccccc">(</span><span style="color:#7ec699">"高楼房子开始打地基"</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
<span style="color:#cccccc">}</span>
<span style="color:#cccccc">@Override</span>
<span style="color:#cc99cd">public</span> <span style="color:#cc99cd">void</span> <span style="color:#f08d49">buildWalls</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span> <span style="color:#cccccc">{</span>
<span style="color:#f8c555">System</span><span style="color:#cccccc">.</span>out<span style="color:#cccccc">.</span><span style="color:#f08d49">println</span><span style="color:#cccccc">(</span><span style="color:#7ec699">"高楼房子开始建墙了"</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
<span style="color:#cccccc">}</span>
<span style="color:#cccccc">@Override</span>
<span style="color:#cc99cd">public</span> <span style="color:#cc99cd">void</span> <span style="color:#f08d49">roofed</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span> <span style="color:#cccccc">{</span>
<span style="color:#f8c555">System</span><span style="color:#cccccc">.</span>out<span style="color:#cccccc">.</span><span style="color:#f08d49">println</span><span style="color:#cccccc">(</span><span style="color:#7ec699">"高楼房子准备封顶喽"</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
<span style="color:#cccccc">}</span>
<span style="color:#cccccc">}</span>
</code></span></span>
最后写一个类用来测试
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-java"><span style="color:#cc99cd">public</span> <span style="color:#cc99cd">class</span> <span style="color:#f8c555">Client</span> <span style="color:#cccccc">{</span>
<span style="color:#cc99cd">public</span> <span style="color:#cc99cd">static</span> <span style="color:#cc99cd">void</span> <span style="color:#f08d49">main</span><span style="color:#cccccc">(</span><span style="color:#f8c555">String</span><span style="color:#cccccc">[</span><span style="color:#cccccc">]</span> args<span style="color:#cccccc">)</span> <span style="color:#cccccc">{</span>
<span style="color:#999999">//准备创建房子的指挥者</span>
<span style="color:#f8c555">HouseDirector</span> houseDirector <span style="color:#67cdcc">=</span> <span style="color:#cc99cd">new</span> <span style="color:#f8c555">HouseDirector</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
<span style="color:#999999">//盖高楼</span>
<span style="color:#f8c555">HighBuilding</span> highBuilding <span style="color:#67cdcc">=</span> <span style="color:#cc99cd">new</span> <span style="color:#f8c555">HighBuilding</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
<span style="color:#999999">//重置建造者</span>
houseDirector<span style="color:#cccccc">.</span><span style="color:#f08d49">setHouseBuilder</span><span style="color:#cccccc">(</span>highBuilding<span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
<span style="color:#999999">//完成盖房子,返回产品(高楼)</span>
houseDirector<span style="color:#cccccc">.</span><span style="color:#f08d49">constructorHouse</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
<span style="color:#cccccc">}</span>
<span style="color:#cccccc">}</span>
</code></span></span>
5、代码结果
6、注意事项
- 每一个具体建造者都相对独立,而与其他的具体建造者无关,因此可以很方便地替换具体建造者或增加新的具体建造者, 用户使用不同的具体建造者即可得到不同的产品对象
- 可以更加精细地控制产品的创建过程 。将复杂产品的创建步骤分解在不同的方法中,使得创建过程更加清晰,也更方便使用程序来控制创建过程
- 增加新的具体建造者无须修改原有类库的代码,指挥者类针对抽象建造者类编程,系统扩展方便,符合 “开闭原则”
- 建造者模式所创建的产品一般具有较多的共同点,其组成部分相似,如果产品之间的差异性很大,则不适合使用建造者模式,因此其使用范围受到一定的限制。
- 如果产品的内部变化复杂,可能会导致需要定义很多具体建造者类来实现这种变化,导致系统变得很庞大,因此在这种情况下,要考虑是否选择建造者模式.
三、总结
那么抽象工厂模式与建造者模式有什么不同点呢?
抽象工厂模式实现对产品家族的创建,一个产品家族是这样的一系列产品:具有不同分类维度的产品组合,采用抽象工厂模式不需要关心构建过程,只关心什么产品,由什么工厂生产即可。而建造者模式则是要求按照指定的蓝图建造产品,它的主要目的是通过组装零配件而产生一个新产品。
更多推荐
所有评论(0)