Skip to content

Commit f5b9814

Browse files
author
周井江-技术中心-服务研发部
committed
initialize
1 parent e13f447 commit f5b9814

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

Decorator/DecoratorPattern.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
interface Component {
2+
void operation();
3+
}
4+
5+
class ConcreteComponent implements Component {
6+
public void operation() {
7+
System.out.println("operation in ConcreteComponent");
8+
}
9+
}
10+
11+
abstract class LogDecrator {
12+
private Component component;
13+
14+
public LogDecrator(Component component) {
15+
if (component == null)
16+
throw new RuntimeException("component == null");
17+
this.component = component;
18+
}
19+
20+
public void operation() {
21+
beforeOperation();
22+
component.operation();
23+
afterOperation();
24+
}
25+
26+
public abstract void beforeOperation();
27+
public abstract void afterOperation();
28+
}
29+
30+
class ConreteLogDecorator extends LogDecrator {
31+
public ConreteLogDecorator(Component component) {
32+
super(component);
33+
}
34+
35+
public void beforeOperation() {
36+
System.out.println("before operation");
37+
}
38+
39+
public void afterOperation() {
40+
System.out.println("after operation");
41+
}
42+
}
43+
44+
class DecoratorPattern {
45+
public static void main(String[] args) {
46+
Component component = new ConcreteComponent();
47+
LogDecrator logDecorator = new ConreteLogDecorator(component);
48+
logDecorator.operation();
49+
}
50+
}
51+

Decorator/DecoratorPattern.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#coding: utf8
2+
3+
import time
4+
5+
"""
6+
python在语言层面就支持装饰器模式,
7+
下面用一个绕口的方式,说明python中的装饰器:
8+
装饰器是一个可调用对象
9+
装饰器需要返回一个可调用对象
10+
装饰器用来装饰可调用对象
11+
python中的可调用对象包括:
12+
函数
13+
方法
14+
实现了__call__方法的对象
15+
16+
装饰器分为两类:
17+
不带参数的装饰器
18+
带参数的装饰器
19+
20+
@decorator
21+
def func():
22+
pass
23+
等同于 func = decorator(func)
24+
25+
@decorator2
26+
@decorator1
27+
def func():
28+
pass
29+
等同于 func = decorator2(decorator1(func))
30+
31+
@decorator(arg)
32+
def func():
33+
pass
34+
等同于 func = decorator(arg)(func)
35+
"""
36+
37+
class LogDecorator:
38+
def __init__(self, wrapped_callable_object):
39+
self._callable = wrapped_callable_object
40+
41+
def __call__(self, *a, **kw):
42+
print("begin to invoked %s with %s, %s" % (self._callable, a, kw))
43+
result = self._callable(*a, **kw)
44+
print("invoked succesfully")
45+
return result
46+
47+
def execution_time_decorator(func):
48+
def _inner(*a, **kw):
49+
start_time = time.time()
50+
result = func(*a, **kw)
51+
print("%s executed in %s s" % (func, time.time()-start_time))
52+
return result
53+
return _inner
54+
55+
if __name__ == "__main__":
56+
@LogDecorator
57+
@execution_time_decorator
58+
def func1():
59+
time.sleep(1)
60+
61+
func1()
62+

Decorator/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
### 简介
2+
3+
装饰器模式可以**在对象的外部,*动态地*扩展对象的功能**
4+
扩展对象的功能,通常有三种方式:
5+
6+
* 直接修改类
7+
* 通过创建派生类来扩展既有类的功能
8+
* 使用组合的方式
9+
10+
在面向对象中,应该**优先使用组合,而不是继承**。装饰模式也是采用组合的方式来扩展对象的功能的。
11+
12+
---
13+
14+
### 角色
15+
16+
* 抽象组件角色
17+
为所有的具体组件角色定义公共的接口
18+
* 具体组件角色
19+
继承或实现抽象组件角色。具体组件对象就是要**被装饰**的对象
20+
* 抽象装饰器角色
21+
为所有的具体装饰器角色定义公共的接口。装饰器对象需要保存**组建对象****引用****如果装饰器不继承组件,那么只能扩展单一的功能****也就是 装饰器对象 不能装饰 其他的 装饰器对象(通俗点说,就是*装饰器对象不能嵌套***
22+
* 具体装饰器角色
23+
实现抽象装饰器角色
24+
25+
---
26+
27+
### UML类图
28+
29+
![decorator.png](http://timd.cn/content/images/pictures/decorator.png)
30+
31+
32+

0 commit comments

Comments
 (0)