Skip to content

Commit 93aa104

Browse files
author
Mike
committed
translate decorator and factory method pattern in Chinese
1 parent 20fac32 commit 93aa104

2 files changed

Lines changed: 227 additions & 0 deletions

File tree

zh/decorator/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: Decorator
4+
folder: decorator
5+
permalink: /patterns/decorator/
6+
categories: Structural
7+
tags:
8+
- Gang Of Four
9+
- Extensibility
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+
程序mple. First of all we have a simple troll implementing the troll interface
37+
38+
```java
39+
public interface Troll {
40+
void attack();
41+
int getAttackPower();
42+
void fleeBattle();
43+
}
44+
45+
public class SimpleTroll implements Troll {
46+
47+
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleTroll.class);
48+
49+
@Override
50+
public void attack() {
51+
LOGGER.info("The troll tries to grab you!");
52+
}
53+
54+
@Override
55+
public int getAttackPower() {
56+
return 10;
57+
}
58+
59+
@Override
60+
public void fleeBattle() {
61+
LOGGER.info("The troll shrieks in horror and runs away!");
62+
}
63+
}
64+
```
65+
66+
下面我们想为巨魔添加球棒。我们可以用装饰者来动态的实现。
67+
68+
```java
69+
public class ClubbedTroll implements Troll {
70+
71+
private static final Logger LOGGER = LoggerFactory.getLogger(ClubbedTroll.class);
72+
73+
private final Troll decorated;
74+
75+
public ClubbedTroll(Troll decorated) {
76+
this.decorated = decorated;
77+
}
78+
79+
@Override
80+
public void attack() {
81+
decorated.attack();
82+
LOGGER.info("The troll swings at you with a club!");
83+
}
84+
85+
@Override
86+
public int getAttackPower() {
87+
return decorated.getAttackPower() + 10;
88+
}
89+
90+
@Override
91+
public void fleeBattle() {
92+
decorated.fleeBattle();
93+
}
94+
}
95+
```
96+
97+
这里是巨魔的实战
98+
99+
```java
100+
// simple troll
101+
var troll = new SimpleTroll();
102+
troll.attack(); // The troll tries to grab you!
103+
troll.fleeBattle(); // The troll shrieks in horror and runs away!
104+
105+
// change the behavior of the simple troll by adding a decorator
106+
var clubbedTroll = new ClubbedTroll(troll);
107+
clubbedTroll.attack(); // The troll tries to grab you! The troll swings at you with a club!
108+
clubbedTroll.fleeBattle(); // The troll shrieks in horror and runs away!
109+
```
110+
111+
## 类图
112+
![alt text](../../decorator/etc/decorator.urm.png "Decorator pattern class diagram")
113+
114+
## 适用性
115+
使用装饰者
116+
117+
* 动态透明地向单个对象添加职责,即不影响其他对象
118+
* 对于可以撤销的责任
119+
* 当通过子类化进行扩展是不切实际的。有时可能会有大量的独立扩展,并且会产生大量的子类来支持每种组合。 否则类定义可能被隐藏或无法用于子类化。
120+
121+
## 教程
122+
* [Decorator Pattern Tutorial](https://www.journaldev.com/1540/decorator-design-pattern-in-java-example)
123+
124+
## Java世界的例子
125+
* [java.io.InputStream](http://docs.oracle.com/javase/8/docs/api/java/io/InputStream.html), [java.io.OutputStream](http://docs.oracle.com/javase/8/docs/api/java/io/OutputStream.html),
126+
[java.io.Reader](http://docs.oracle.com/javase/8/docs/api/java/io/Reader.html) and [java.io.Writer](http://docs.oracle.com/javase/8/docs/api/java/io/Writer.html)
127+
* [java.util.Collections#synchronizedXXX()](http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#synchronizedCollection-java.util.Collection-)
128+
* [java.util.Collections#unmodifiableXXX()](http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#unmodifiableCollection-java.util.Collection-)
129+
* [java.util.Collections#checkedXXX()](http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#checkedCollection-java.util.Collection-java.lang.Class-)
130+
131+
132+
## 鸣谢
133+
134+
* [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)
135+
* [Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions](https://www.amazon.com/gp/product/1937785467/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1937785467&linkCode=as2&tag=javadesignpat-20&linkId=7e4e2fb7a141631491534255252fd08b)
136+
* [J2EE Design Patterns](https://www.amazon.com/gp/product/0596004273/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596004273&linkCode=as2&tag=javadesignpat-20&linkId=48d37c67fb3d845b802fa9b619ad8f31)
137+
* [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)
138+
* [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)
139+
* [J2EE Design Patterns](https://www.amazon.com/gp/product/0596004273/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596004273&linkCode=as2&tag=javadesignpat-20&linkId=f27d2644fbe5026ea448791a8ad09c94)

zh/factory-method/README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
layout: pattern
3+
title: Factory Method
4+
folder: factory-method
5+
permalink: /patterns/factory-method/
6+
categories: Creational
7+
tags:
8+
- Extensibility
9+
- Gang Of Four
10+
---
11+
12+
## Also known as
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 Blacksmith {
39+
Weapon manufactureWeapon(WeaponType weaponType);
40+
}
41+
42+
public class ElfBlacksmith implements Blacksmith {
43+
public Weapon manufactureWeapon(WeaponType weaponType) {
44+
return ELFARSENAL.get(weaponType);
45+
}
46+
}
47+
48+
public class OrcBlacksmith implements Blacksmith {
49+
public Weapon manufactureWeapon(WeaponType weaponType) {
50+
return ORCARSENAL.get(weaponType);
51+
}
52+
}
53+
```
54+
55+
现在随着客户的到来,会召唤出正确类型的铁匠并制造出要求的武器。
56+
57+
```java
58+
var blacksmith = new ElfBlacksmith();
59+
blacksmith.manufactureWeapon(WeaponType.SPEAR);
60+
blacksmith.manufactureWeapon(WeaponType.AXE);
61+
// Elvish weapons are created
62+
```
63+
64+
## 类图
65+
![alt text](../../factory-method/etc/factory-method.urm.png "Factory Method pattern class diagram")
66+
67+
## 适用性
68+
使用工厂方法模式当
69+
70+
* 一个类无法预料它所要必须创建的对象的类
71+
* 一个类想要它的子类来指定它要创建的对象
72+
* 类将责任委派给几个帮助子类中的一个,而你想定位了解是具体之中的哪一个
73+
74+
## Java中的例子
75+
76+
* [java.util.Calendar](http://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#getInstance--)
77+
* [java.util.ResourceBundle](http://docs.oracle.com/javase/8/docs/api/java/util/ResourceBundle.html#getBundle-java.lang.String-)
78+
* [java.text.NumberFormat](http://docs.oracle.com/javase/8/docs/api/java/text/NumberFormat.html#getInstance--)
79+
* [java.nio.charset.Charset](http://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html#forName-java.lang.String-)
80+
* [java.net.URLStreamHandlerFactory](http://docs.oracle.com/javase/8/docs/api/java/net/URLStreamHandlerFactory.html#createURLStreamHandler-java.lang.String-)
81+
* [java.util.EnumSet](https://docs.oracle.com/javase/8/docs/api/java/util/EnumSet.html#of-E-)
82+
* [javax.xml.bind.JAXBContext](https://docs.oracle.com/javase/8/docs/api/javax/xml/bind/JAXBContext.html#createMarshaller--)
83+
84+
## 鸣谢
85+
86+
* [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)
87+
* [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)
88+
* [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

Comments
 (0)