-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate_method.py
More file actions
71 lines (51 loc) · 1.41 KB
/
template_method.py
File metadata and controls
71 lines (51 loc) · 1.41 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
python_pattern.examples.adapter
~~~~~~~~~~~
模版方法模式
定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。 TemplateMethod使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。
:copyright: (c) 2017 by the yinyi.
:license: BSD, see LICENSE for more details.
"""
from cowpy import cow
def dots_style(msg):
msg = msg.capitalize()
msg = '.' * 10 + msg + '.' * 10
return msg
def admire_style(msg):
msg = msg.upper()
return '!'.join(msg)
def cow_style(msg):
msg = cow.milk_random_cow(msg)
return msg
def generate_banner(msg, style=dots_style):
print('-- start of banner --')
print(style(msg))
print('-- end of banner --\n\n')
def main():
msg = 'happy coding'
[generate_banner(msg, style) for style in (dots_style, admire_style,
cow_style)]
if __name__ == "__main__":
main()
"""
-- start of banner --
..........Happy coding..........
-- end of banner --
-- start of banner --
H!A!P!P!Y! !C!O!D!I!N!G
-- end of banner --
-- start of banner --
______________
< happy coding >
--------------
o
o
^__^ /
(**)\_______/ _________
(__)\ )=( ____|_ \_____
U ||----w | \ \ \_____ |
|| || || ||
-- end of banner --
"""