Skip to content

Commit e9f73bc

Browse files
sudowandererMikeiluwatar
authored
Translation zh (iluwatar#1610)
* add state and callback pattern * add command and template-method pattern * add iterator pattern * add bridege and DI pattern * fix issue iluwatar#1600 Co-authored-by: Mike <admin@xiaod.info> Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
1 parent 29ceac2 commit e9f73bc

File tree

3 files changed

+306
-2
lines changed

3 files changed

+306
-2
lines changed

zh/bridge/README.md

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
---
2+
layout: pattern
3+
title: Bridge
4+
folder: bridge
5+
permalink: /patterns/bridge/
6+
categories: Structural
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+
33+
**程序示例**
34+
35+
翻译一下上面的武器示例。下面我们有武器的类层级:
36+
37+
```java
38+
public interface Weapon {
39+
void wield();
40+
void swing();
41+
void unwield();
42+
Enchantment getEnchantment();
43+
}
44+
45+
public class Sword implements Weapon {
46+
47+
private final Enchantment enchantment;
48+
49+
public Sword(Enchantment enchantment) {
50+
this.enchantment = enchantment;
51+
}
52+
53+
@Override
54+
public void wield() {
55+
LOGGER.info("The sword is wielded.");
56+
enchantment.onActivate();
57+
}
58+
59+
@Override
60+
public void swing() {
61+
LOGGER.info("The sword is swinged.");
62+
enchantment.apply();
63+
}
64+
65+
@Override
66+
public void unwield() {
67+
LOGGER.info("The sword is unwielded.");
68+
enchantment.onDeactivate();
69+
}
70+
71+
@Override
72+
public Enchantment getEnchantment() {
73+
return enchantment;
74+
}
75+
}
76+
77+
public class Hammer implements Weapon {
78+
79+
private final Enchantment enchantment;
80+
81+
public Hammer(Enchantment enchantment) {
82+
this.enchantment = enchantment;
83+
}
84+
85+
@Override
86+
public void wield() {
87+
LOGGER.info("The hammer is wielded.");
88+
enchantment.onActivate();
89+
}
90+
91+
@Override
92+
public void swing() {
93+
LOGGER.info("The hammer is swinged.");
94+
enchantment.apply();
95+
}
96+
97+
@Override
98+
public void unwield() {
99+
LOGGER.info("The hammer is unwielded.");
100+
enchantment.onDeactivate();
101+
}
102+
103+
@Override
104+
public Enchantment getEnchantment() {
105+
return enchantment;
106+
}
107+
}
108+
```
109+
110+
这里是单独的附魔类结构:
111+
112+
```java
113+
public interface Enchantment {
114+
void onActivate();
115+
void apply();
116+
void onDeactivate();
117+
}
118+
119+
public class FlyingEnchantment implements Enchantment {
120+
121+
@Override
122+
public void onActivate() {
123+
LOGGER.info("The item begins to glow faintly.");
124+
}
125+
126+
@Override
127+
public void apply() {
128+
LOGGER.info("The item flies and strikes the enemies finally returning to owner's hand.");
129+
}
130+
131+
@Override
132+
public void onDeactivate() {
133+
LOGGER.info("The item's glow fades.");
134+
}
135+
}
136+
137+
public class SoulEatingEnchantment implements Enchantment {
138+
139+
@Override
140+
public void onActivate() {
141+
LOGGER.info("The item spreads bloodlust.");
142+
}
143+
144+
@Override
145+
public void apply() {
146+
LOGGER.info("The item eats the soul of enemies.");
147+
}
148+
149+
@Override
150+
public void onDeactivate() {
151+
LOGGER.info("Bloodlust slowly disappears.");
152+
}
153+
}
154+
```
155+
156+
这里是两种层次结构的实践:
157+
158+
```java
159+
var enchantedSword = new Sword(new SoulEatingEnchantment());
160+
enchantedSword.wield();
161+
enchantedSword.swing();
162+
enchantedSword.unwield();
163+
// The sword is wielded.
164+
// The item spreads bloodlust.
165+
// The sword is swinged.
166+
// The item eats the soul of enemies.
167+
// The sword is unwielded.
168+
// Bloodlust slowly disappears.
169+
170+
var hammer = new Hammer(new FlyingEnchantment());
171+
hammer.wield();
172+
hammer.swing();
173+
hammer.unwield();
174+
// The hammer is wielded.
175+
// The item begins to glow faintly.
176+
// The hammer is swinged.
177+
// The item flies and strikes the enemies finally returning to owner's hand.
178+
// The hammer is unwielded.
179+
// The item's glow fades.
180+
```
181+
182+
183+
184+
## 类图
185+
186+
![alt text](../../bridge/etc/bridge.urm.png "Bridge class diagram")
187+
188+
## 适用性
189+
190+
使用桥接模式当
191+
192+
* 你想永久性的避免抽象和他的实现之间的绑定。有可能是这种情况,当实现需要被选择或者在运行时切换。
193+
* 抽象和他们的实现应该能通过写子类来扩展。这种情况下,桥接模式让你可以组合不同的抽象和实现并独立的扩展他们。
194+
* 对抽象的实现的改动应当不会对客户产生影响;也就是说,他们的代码不必重新编译。
195+
* 你有种类繁多的类。这样的类层次结构表明需要将一个对象分为两部分。Rumbaugh 使用术语“嵌套归纳”来指代这种类层次结构。
196+
* 你想在多个对象间分享一种实现(可能使用引用计数),这个事实应该对客户隐藏。一个简单的示例是Coplien的String类,其中多个对象可以共享同一字符串表示形式
197+
198+
## 教程
199+
200+
* [Bridge Pattern Tutorial](https://www.journaldev.com/1491/bridge-design-pattern-java)
201+
202+
## 鸣谢
203+
204+
* [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)
205+
* [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)

zh/decorator/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ tags:
3333

3434
以巨魔的为例。首先我有有一个简单的巨魔,实现了巨魔接口。
3535

36-
程序mple. First of all we have a simple troll implementing the troll interface
37-
3836
```java
3937
public interface Troll {
4038
void attack();

zh/dependency-injection/README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
layout: pattern
3+
title: Dependency Injection
4+
folder: dependency-injection
5+
permalink: /patterns/dependency-injection/
6+
categories: Creational
7+
tags:
8+
- Decoupling
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+
```java
34+
public abstract class Tobacco {
35+
36+
private static final Logger LOGGER = LoggerFactory.getLogger(Tobacco.class);
37+
38+
public void smoke(Wizard wizard) {
39+
LOGGER.info("{} smoking {}", wizard.getClass().getSimpleName(),
40+
this.getClass().getSimpleName());
41+
}
42+
}
43+
44+
public class SecondBreakfastTobacco extends Tobacco {
45+
}
46+
47+
public class RivendellTobacco extends Tobacco {
48+
}
49+
50+
public class OldTobyTobacco extends Tobacco {
51+
}
52+
```
53+
54+
下面是老巫师的类的层次结构。
55+
56+
```java
57+
public interface Wizard {
58+
59+
void smoke();
60+
}
61+
62+
public class AdvancedWizard implements Wizard {
63+
64+
private final Tobacco tobacco;
65+
66+
public AdvancedWizard(Tobacco tobacco) {
67+
this.tobacco = tobacco;
68+
}
69+
70+
@Override
71+
public void smoke() {
72+
tobacco.smoke(this);
73+
}
74+
}
75+
```
76+
77+
最后我们可以看到给老巫师任意品牌的烟草是多么的简单。
78+
79+
```java
80+
var advancedWizard = new AdvancedWizard(new SecondBreakfastTobacco());
81+
advancedWizard.smoke();
82+
```
83+
84+
## 类图
85+
86+
![alt text](../../dependency-injection/etc/dependency-injection.png "Dependency Injection")
87+
88+
## 适用性
89+
90+
使用依赖注入当:
91+
92+
- 当你需要从对象中移除掉具体的实现内容时
93+
94+
* 使用模拟对象或存根隔离地启用类的单元测试
95+
96+
## 鸣谢
97+
98+
* [Dependency Injection Principles, Practices, and Patterns](https://www.amazon.com/gp/product/161729473X/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=161729473X&linkId=57079257a5c7d33755493802f3b884bd)
99+
* [Clean Code: A Handbook of Agile Software Craftsmanship](https://www.amazon.com/gp/product/0132350882/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0132350882&linkCode=as2&tag=javadesignpat-20&linkId=2c390d89cc9e61c01b9e7005c7842871)
100+
* [Java 9 Dependency Injection: Write loosely coupled code with Spring 5 and Guice](https://www.amazon.com/gp/product/1788296257/ref=as_li_tl?ie=UTF8&tag=javadesignpat-20&camp=1789&creative=9325&linkCode=as2&creativeASIN=1788296257&linkId=4e9137a3bf722a8b5b156cce1eec0fc1)
101+
* [Google Guice Tutorial: Open source Java based dependency injection framework](https://www.amazon.com/gp/product/B083P7DZ8M/ref=as_li_tl?ie=UTF8&tag=javadesignpat-20&camp=1789&creative=9325&linkCode=as2&creativeASIN=B083P7DZ8M&linkId=04f0f902c877921e45215b624a124bfe)

0 commit comments

Comments
 (0)