Skip to content

Commit adc267e

Browse files
authored
Merge pull request iluwatar#1526 from xiaod-dev/translation-zh
Translation zh
2 parents cc8d209 + dac8e65 commit adc267e

File tree

5 files changed

+732
-0
lines changed

5 files changed

+732
-0
lines changed

zh/builder/README.md

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
![alt text](../../builder/etc/builder.urm.png "Builder class diagram")
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)

zh/chain/README.md

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
---
2+
layout: pattern
3+
title: Chain of responsibility
4+
folder: chain
5+
permalink: /patterns/chain/
6+
categories: Behavioral
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 class Request {
34+
35+
private final RequestType requestType;
36+
private final String requestDescription;
37+
private boolean handled;
38+
39+
public Request(final RequestType requestType, final String requestDescription) {
40+
this.requestType = Objects.requireNonNull(requestType);
41+
this.requestDescription = Objects.requireNonNull(requestDescription);
42+
}
43+
44+
public String getRequestDescription() { return requestDescription; }
45+
46+
public RequestType getRequestType() { return requestType; }
47+
48+
public void markHandled() { this.handled = true; }
49+
50+
public boolean isHandled() { return this.handled; }
51+
52+
@Override
53+
public String toString() { return getRequestDescription(); }
54+
}
55+
56+
public enum RequestType {
57+
DEFEND_CASTLE, TORTURE_PRISONER, COLLECT_TAX
58+
}
59+
```
60+
61+
然后是请求处理器的层次结构
62+
63+
```java
64+
public abstract class RequestHandler {
65+
private static final Logger LOGGER = LoggerFactory.getLogger(RequestHandler.class);
66+
private final RequestHandler next;
67+
68+
public RequestHandler(RequestHandler next) {
69+
this.next = next;
70+
}
71+
72+
public void handleRequest(Request req) {
73+
if (next != null) {
74+
next.handleRequest(req);
75+
}
76+
}
77+
78+
protected void printHandling(Request req) {
79+
LOGGER.info("{} handling request \"{}\"", this, req);
80+
}
81+
82+
@Override
83+
public abstract String toString();
84+
}
85+
86+
public class OrcCommander extends RequestHandler {
87+
public OrcCommander(RequestHandler handler) {
88+
super(handler);
89+
}
90+
91+
@Override
92+
public void handleRequest(Request req) {
93+
if (req.getRequestType().equals(RequestType.DEFEND_CASTLE)) {
94+
printHandling(req);
95+
req.markHandled();
96+
} else {
97+
super.handleRequest(req);
98+
}
99+
}
100+
101+
@Override
102+
public String toString() {
103+
return "Orc commander";
104+
}
105+
}
106+
107+
// OrcOfficer和OrcSoldier的定义与OrcCommander类似
108+
109+
```
110+
111+
然后我们有兽王下达命令并形成链条
112+
113+
```java
114+
public class OrcKing {
115+
RequestHandler chain;
116+
117+
public OrcKing() {
118+
buildChain();
119+
}
120+
121+
private void buildChain() {
122+
chain = new OrcCommander(new OrcOfficer(new OrcSoldier(null)));
123+
}
124+
125+
public void makeRequest(Request req) {
126+
chain.handleRequest(req);
127+
}
128+
}
129+
```
130+
131+
然后这样使用它
132+
133+
```java
134+
var king = new OrcKing();
135+
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle")); // Orc commander handling request "defend castle"
136+
king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner")); // Orc officer handling request "torture prisoner"
137+
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax")); // Orc soldier handling request "collect tax"
138+
```
139+
140+
## 类图
141+
![alt text](../../chain/etc/chain.urm.png "Chain of Responsibility class diagram")
142+
143+
## 适用性
144+
使用责任链模式当
145+
146+
* 多于一个对象可能要处理请求,并且处理器并不知道一个优先级。处理器应自动确定。
147+
* 你想对多个对象之一发出请求而无需明确指定接收者
148+
* 处理请求的对象集合应该被动态指定时
149+
150+
## Java世界例子
151+
152+
* [java.util.logging.Logger#log()](http://docs.oracle.com/javase/8/docs/api/java/util/logging/Logger.html#log%28java.util.logging.Level,%20java.lang.String%29)
153+
* [Apache Commons Chain](https://commons.apache.org/proper/commons-chain/index.html)
154+
* [javax.servlet.Filter#doFilter()](http://docs.oracle.com/javaee/7/api/javax/servlet/Filter.html#doFilter-javax.servlet.ServletRequest-javax.servlet.ServletResponse-javax.servlet.FilterChain-)
155+
156+
## 鸣谢
157+
158+
* [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)
159+
* [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)

0 commit comments

Comments
 (0)