Skip to content

Commit 5a633ac

Browse files
authored
translation: add Chinese translation for servant design pattern (#2804)
1 parent c702df0 commit 5a633ac

File tree

2 files changed

+230
-0
lines changed

2 files changed

+230
-0
lines changed

localization/zh/servant/README.md

+230
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
---
2+
title: Servant
3+
category: Behavioral
4+
language: zh
5+
tag:
6+
- Decoupling
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+
```java
32+
/**
33+
* Servant.
34+
*/
35+
public class Servant {
36+
37+
public String name;
38+
39+
/**
40+
* Constructor.
41+
*/
42+
public Servant(String name) {
43+
this.name = name;
44+
}
45+
46+
public void feed(Royalty r) {
47+
r.getFed();
48+
}
49+
50+
public void giveWine(Royalty r) {
51+
r.getDrink();
52+
}
53+
54+
public void giveCompliments(Royalty r) {
55+
r.receiveCompliments();
56+
}
57+
58+
/**
59+
* Check if we will be hanged.
60+
*/
61+
public boolean checkIfYouWillBeHanged(List<Royalty> tableGuests) {
62+
return tableGuests.stream().allMatch(Royalty::getMood);
63+
}
64+
}
65+
```
66+
67+
皇家是一个接口,它被国王和女王类实现,以获取仆人的服务
68+
69+
```java
70+
interface Royalty {
71+
72+
void getFed();
73+
74+
void getDrink();
75+
76+
void changeMood();
77+
78+
void receiveCompliments();
79+
80+
boolean getMood();
81+
}
82+
```
83+
国王类正在实现皇家接口
84+
```java
85+
public class King implements Royalty {
86+
87+
private boolean isDrunk;
88+
private boolean isHungry = true;
89+
private boolean isHappy;
90+
private boolean complimentReceived;
91+
92+
@Override
93+
public void getFed() {
94+
isHungry = false;
95+
}
96+
97+
@Override
98+
public void getDrink() {
99+
isDrunk = true;
100+
}
101+
102+
public void receiveCompliments() {
103+
complimentReceived = true;
104+
}
105+
106+
@Override
107+
public void changeMood() {
108+
if (!isHungry && isDrunk) {
109+
isHappy = true;
110+
}
111+
if (complimentReceived) {
112+
isHappy = false;
113+
}
114+
}
115+
116+
@Override
117+
public boolean getMood() {
118+
return isHappy;
119+
}
120+
}
121+
```
122+
女王类正在实现皇家接口
123+
```java
124+
public class Queen implements Royalty {
125+
126+
private boolean isDrunk = true;
127+
private boolean isHungry;
128+
private boolean isHappy;
129+
private boolean isFlirty = true;
130+
private boolean complimentReceived;
131+
132+
@Override
133+
public void getFed() {
134+
isHungry = false;
135+
}
136+
137+
@Override
138+
public void getDrink() {
139+
isDrunk = true;
140+
}
141+
142+
public void receiveCompliments() {
143+
complimentReceived = true;
144+
}
145+
146+
@Override
147+
public void changeMood() {
148+
if (complimentReceived && isFlirty && isDrunk && !isHungry) {
149+
isHappy = true;
150+
}
151+
}
152+
153+
@Override
154+
public boolean getMood() {
155+
return isHappy;
156+
}
157+
158+
public void setFlirtiness(boolean f) {
159+
this.isFlirty = f;
160+
}
161+
162+
}
163+
```
164+
165+
然后,为了使用:
166+
167+
```java
168+
public class App {
169+
170+
private static final Servant jenkins = new Servant("Jenkins");
171+
private static final Servant travis = new Servant("Travis");
172+
173+
/**
174+
* Program entry point.
175+
*/
176+
public static void main(String[] args) {
177+
scenario(jenkins, 1);
178+
scenario(travis, 0);
179+
}
180+
181+
/**
182+
* Can add a List with enum Actions for variable scenarios.
183+
*/
184+
public static void scenario(Servant servant, int compliment) {
185+
var k = new King();
186+
var q = new Queen();
187+
188+
var guests = List.of(k, q);
189+
190+
// feed
191+
servant.feed(k);
192+
servant.feed(q);
193+
// serve drinks
194+
servant.giveWine(k);
195+
servant.giveWine(q);
196+
// compliment
197+
servant.giveCompliments(guests.get(compliment));
198+
199+
// outcome of the night
200+
guests.forEach(Royalty::changeMood);
201+
202+
// check your luck
203+
if (servant.checkIfYouWillBeHanged(guests)) {
204+
LOGGER.info("{} will live another day", servant.name);
205+
} else {
206+
LOGGER.info("Poor {}. His days are numbered", servant.name);
207+
}
208+
}
209+
}
210+
```
211+
212+
程序输出
213+
214+
```
215+
Jenkins will live another day
216+
Poor Travis. His days are numbered
217+
```
218+
219+
220+
## 类图
221+
![alt text](./etc/servant-pattern.png "Servant")
222+
223+
## 适用场景
224+
在什么时候使用仆人模式
225+
226+
* 当我们希望某些对象执行一个公共操作并且不想将该操作定义为每个类中的方法时
227+
228+
## 鸣谢
229+
230+
* [Let's Modify the Objects-First Approach into Design-Patterns-First](http://edu.pecinovsky.cz/papers/2006_ITiCSE_Design_Patterns_First.pdf)
29.4 KB
Loading

0 commit comments

Comments
 (0)