forked from yidao620c/python3-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd04_template_method.py
More file actions
48 lines (37 loc) · 962 Bytes
/
Copy pathd04_template_method.py
File metadata and controls
48 lines (37 loc) · 962 Bytes
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
Topic: 模板方法模式
Desc :
"""
class AbstractTemplate:
# 基本方法1
def do_something(self):
pass
# 基本方法2
def do_anything(self):
pass
# 模板方法
def template_method(self):
# 调用基本方法,完成相关的业务逻辑
self.do_something()
self.do_anything()
class ConcreteClass1(AbstractTemplate):
# 基本方法1
def do_something(self):
print('class1 doSomething...')
# 基本方法2
def do_anything(self):
print('class1 doAnything...')
class ConcreteClass2(AbstractTemplate):
# 基本方法1
def do_something(self):
print('class2 doSomething...')
# 基本方法2
def do_anything(self):
print('class2 doAnything...')
if __name__ == '__main__':
c1 = ConcreteClass1()
c1.template_method()
c2 = ConcreteClass2()
c2.template_method()