|
| 1 | +--- |
| 2 | +layout: pattern |
| 3 | +title: Builder |
| 4 | +folder: builder |
| 5 | +permalink: /patterns/builder/ |
| 6 | +categories: Creational |
| 7 | +tags: |
| 8 | + - Gang of Four |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +## 目的 |
| 13 | + |
| 14 | +将复杂对象的构造与其表示分开,以便同一构造过程可以创建不同的表示。 |
| 15 | + |
| 16 | +## 解释 |
| 17 | + |
| 18 | +现实世界例子 |
| 19 | + |
| 20 | +> 想象一个角色扮演游戏的角色生成器。最简单的选择是让计算机为你创建角色。但是如果你想选择一些像专业,性别,发色等角色细节时,这个角色生成就变成了一个渐进的过程。当所有选择完成时,该过程也将完成。 |
| 21 | +
|
| 22 | +用通俗的话说 |
| 23 | + |
| 24 | +> 允许你创建不同口味的对象同时避免构造器污染。当一个对象可能有几种口味,或者一个对象的创建涉及到很多步骤时会很有用。 |
| 25 | +
|
| 26 | +维基百科说 |
| 27 | + |
| 28 | +> 建造者模式是一种对象创建的软件设计模式,旨在为伸缩构造器反模式寻找一个解决方案。 |
| 29 | +
|
| 30 | +说了这么多,让我补充一下什么是伸缩构造函数反模式。我们肯定都见过像下面这样的构造器: |
| 31 | + |
| 32 | +```java |
| 33 | +public Hero(Profession profession, String name, HairType hairType, HairColor hairColor, Armor armor, Weapon weapon) { |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +就像你看到的构造器参数的数量很快就会失控同时参数的排列方式可能变得难以理解。另外,如果您将来希望添加更多选项,则此参数列表可能会继续增长。这就被称伸缩构造器反模式。 |
| 38 | + |
| 39 | +**编程示例** |
| 40 | + |
| 41 | +明智的选择是使用建造者模式。首先我们有一个英雄要创建。 |
| 42 | + |
| 43 | +```java |
| 44 | +public final class Hero { |
| 45 | + private final Profession profession; |
| 46 | + private final String name; |
| 47 | + private final HairType hairType; |
| 48 | + private final HairColor hairColor; |
| 49 | + private final Armor armor; |
| 50 | + private final Weapon weapon; |
| 51 | + |
| 52 | + private Hero(Builder builder) { |
| 53 | + this.profession = builder.profession; |
| 54 | + this.name = builder.name; |
| 55 | + this.hairColor = builder.hairColor; |
| 56 | + this.hairType = builder.hairType; |
| 57 | + this.weapon = builder.weapon; |
| 58 | + this.armor = builder.armor; |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +然后我们有创建者 |
| 64 | + |
| 65 | +```java |
| 66 | + public static class Builder { |
| 67 | + private final Profession profession; |
| 68 | + private final String name; |
| 69 | + private HairType hairType; |
| 70 | + private HairColor hairColor; |
| 71 | + private Armor armor; |
| 72 | + private Weapon weapon; |
| 73 | + |
| 74 | + public Builder(Profession profession, String name) { |
| 75 | + if (profession == null || name == null) { |
| 76 | + throw new IllegalArgumentException("profession and name can not be null"); |
| 77 | + } |
| 78 | + this.profession = profession; |
| 79 | + this.name = name; |
| 80 | + } |
| 81 | + |
| 82 | + public Builder withHairType(HairType hairType) { |
| 83 | + this.hairType = hairType; |
| 84 | + return this; |
| 85 | + } |
| 86 | + |
| 87 | + public Builder withHairColor(HairColor hairColor) { |
| 88 | + this.hairColor = hairColor; |
| 89 | + return this; |
| 90 | + } |
| 91 | + |
| 92 | + public Builder withArmor(Armor armor) { |
| 93 | + this.armor = armor; |
| 94 | + return this; |
| 95 | + } |
| 96 | + |
| 97 | + public Builder withWeapon(Weapon weapon) { |
| 98 | + this.weapon = weapon; |
| 99 | + return this; |
| 100 | + } |
| 101 | + |
| 102 | + public Hero build() { |
| 103 | + return new Hero(this); |
| 104 | + } |
| 105 | + } |
| 106 | +``` |
| 107 | + |
| 108 | +然后可以这样使用 |
| 109 | + |
| 110 | +```java |
| 111 | +var mage = new Hero.Builder(Profession.MAGE, "Riobard").withHairColor(HairColor.BLACK).withWeapon(Weapon.DAGGER).build(); |
| 112 | +``` |
| 113 | + |
| 114 | +## 类图 |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +## 适用性 |
| 119 | + |
| 120 | +使用建造者模式当 |
| 121 | + |
| 122 | +* 创建复杂对象的算法应独立于组成对象的零件及其组装方式 |
| 123 | +* 构造过程必须允许所构造的对象具有不同的表示形式 |
| 124 | + |
| 125 | +## Java世界例子 |
| 126 | + |
| 127 | +* [java.lang.StringBuilder](http://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html) |
| 128 | +* [java.nio.ByteBuffer](http://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html#put-byte-) as well as similar buffers such as FloatBuffer, IntBuffer and so on. |
| 129 | +* [java.lang.StringBuffer](http://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html#append-boolean-) |
| 130 | +* All implementations of [java.lang.Appendable](http://docs.oracle.com/javase/8/docs/api/java/lang/Appendable.html) |
| 131 | +* [Apache Camel builders](https://github.com/apache/camel/tree/0e195428ee04531be27a0b659005e3aa8d159d23/camel-core/src/main/java/org/apache/camel/builder) |
| 132 | +* [Apache Commons Option.Builder](https://commons.apache.org/proper/commons-cli/apidocs/org/apache/commons/cli/Option.Builder.html) |
| 133 | + |
| 134 | +## 鸣谢 |
| 135 | + |
| 136 | +* [Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59) |
| 137 | +* [Effective Java](https://www.amazon.com/gp/product/0134685997/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0134685997&linkCode=as2&tag=javadesignpat-20&linkId=4e349f4b3ff8c50123f8147c828e53eb) |
| 138 | +* [Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b) |
| 139 | +* [Refactoring to Patterns](https://www.amazon.com/gp/product/0321213351/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0321213351&linkCode=as2&tag=javadesignpat-20&linkId=2a76fcb387234bc71b1c61150b3cc3a7) |
0 commit comments