Skip to content

Commit c535805

Browse files
committed
更新了五个段落
1 parent 3b42235 commit c535805

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

第五章-装饰器模式.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,36 @@
33

44
Whenever we want to add extra functionality to an object, we have a number of different options. We can:
55

6+
任何时候,只要我们想给一个对象添加附加的功能,就有多个不同的选项供我们选择。我们可以选择:
7+
68
```
79
Add the functionality directly to the class the object belongs to, if it makes sense (for example, add a new method)
810
Use composition
911
Use inheritance
1012
```
1113

14+
```
15+
直接对类的对象添加功能,如果可行的话(例如,添加一个新方法)
16+
合成
17+
继承
18+
```
19+
1220
Composition should generally be preferred over inheritance, because inheritance makes code reuse harder, it's static, and applies to an entire class and all instances of it [GOF95, page 31], [j.mp/decopat].
1321

22+
通常对合成的选择应该优先于继承,因为继承会使代码很难重复使用,而且继承是静态的,它会被应用到整个类,以及类的全部实例。
23+
1424
Design patterns offer us a fourth option that supports extending the functionality of an object dynamically (in runtime): Decorators. A Decorator pattern can add responsibilities to an object dynamically, and in a transparent manner (without affecting other objects) [GOF95, page 196].
1525

26+
设计模式给我们动态地(运行时)提供了扩展一个对象的第四个选择:装饰器。装饰器模式能够动态地给一个对象添加信任,而且是使用透明模式(不会影响到其他的对象)。
27+
1628
In many programming languages, the Decorator pattern is implemented using sub-classing (inheritance) [GOF95, page 198]. In Python, we can (and should) use the built-in decorator feature. A Python decorator is a specific change to the syntax of Python that is used for extending the behavior of a class, method, or function without using inheritance. In terms of implementation, a Python decorator is a callable (function, method, class) that accepts a function object fin as input, and returns another function object fout [j.mp/conqdec]. This means that any callable that has these properties can be treated as a decorator. We have already seen how to use the built-in property decorator that makes a method appear as a variable in Chapter 1, The Factory Pattern and Chapter 2, The Builder Pattern. In the implementation section, we will learn how to implement and use our own decorators.
1729

30+
在很多的编程语言中,装饰器模式是通过使用子类化(继承)实现的[GOF95,198页]。在Python中我们可以(而且应该)使用内建的装饰器功能。Python装饰器是专门用来改变Python语法的,它用来扩展一个类的行为,方法,或者函数不需要用到继承。就实现的术语观点来看,Python装饰器是一个可调用的对象(函数、方法、类)能够接受函数对象作为输入,并返回另外一个不同的函数。这就意味着任何一个拥有这些特性的可调用对象都可以被当作装饰器。我们已经在第一章《工厂模式》和第二章《构造器模式》见过了如何使用内建的特性装饰器将一个方法以变量的形式出现。在具体实现部分,我们会学习到如何实现并利用装饰器。
31+
1832
There is no one-to-one relationship between the Decorator pattern and Python decorators. Python decorators can actually do much more than the Decorator pattern. One of the things they can be used for, is to implement the Decorator pattern [Eckel08, page 59], [j.mp/moinpydec].
1933

34+
Python的装饰器和装饰器模式之间不存在一对一的关系。Python装饰器实际上能做的事情比装饰器模式多得多。使用Python装饰器的其中一个目的就是实现装饰器模式[Eckel08, 59页]。
35+
2036
## 真实的例子
2137
模式被称做装饰器实际上并不意味着它只应该让事情更好些。通常装饰器模式用来扩展一个对象的功能。这样的扩展的真实例子是:给一把枪添加一个消音器,使用不同的相机镜头(可移除的镜头),等等。
2238

0 commit comments

Comments
 (0)