-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy.py
80 lines (56 loc) · 1.91 KB
/
strategy.py
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
72
73
74
75
76
77
78
79
80
# coding: utf-8
class Report(object):
def __init__(self, title, text, formatter):
self._title = title
self._text = text
self._formatter = formatter
def output_report(self):
self._formatter.output_report(self._title, self._text)
class Formatter(object):
def output_report(self, title, text):
assert False
class HTMLFormatter(Formatter):
def output_report(self, title, text):
print('<html>')
print('<head>')
print('<title>{}</title>'.format(title))
print('</head>')
print('<body>')
for line in text:
print("<p>{}</p>".format(line))
print('</body>')
print('</html>')
class PlainTextFormatter(Formatter):
def output_report(self, title, text):
print('*** {} ***'.format(title))
for line in text:
print(line)
class CallableReport(object):
def __init__(self, title, text, formatter):
self._title = title
self._text = text
self._formatter = formatter
def output_report(self):
self._formatter(self._title, self._text)
class CallableFormatter(object):
def __init__(self, decoration='***'):
self._decoration = decoration
def __call__(self, title, text):
print('{decoration}{content}{decoration}'.format(
decoration=self._decoration,
content=title)
)
for line in text:
print(line)
if __name__ == '__main__':
report = Report('月次報告', ['順調!', '最高です!'],
PlainTextFormatter())
report.output_report()
print('-' * 50)
report = Report('月次報告', ['順調!', '最高です!'],
HTMLFormatter())
report.output_report()
print('-' * 50)
report = CallableReport('月次報告', ['順調!', '最高です!'],
CallableFormatter())
report.output_report()