This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
template_method.py
74 lines (54 loc) · 1.87 KB
/
template_method.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
"""Template method pattern
Template Method is a behavioral design pattern. It defines an algorithm's
skeleton in a Base class, but lets subclasses redefine certain steps of the
algorithm. The Base class declares some placeholder methods, and derived classes
implement them.
"""
import sys
from abc import ABC, abstractmethod
class Algorithm(ABC):
def template_method(self):
"""Skeleton of operations to perform. DON'T override me.
The Template Method defines a skeleton of an algorithm in an operation,
and defers some steps to subclasses.
"""
self.__do_absolutely_this()
self.do_step_1()
self.do_step_2()
self.do_something()
def __do_absolutely_this(self):
"""Protected operation. DON'T override me."""
this_method_name = sys._getframe().f_code.co_name
print("{}.{}".format(self.__class__.__name__, this_method_name))
@abstractmethod
def do_step_1(self):
"""Primitive operation. You HAVE TO override me, I'm a placeholder."""
pass
@abstractmethod
def do_step_2(self):
"""Primitive operation. You HAVE TO override me, I'm a placeholder."""
pass
def do_something(self):
"""Hook. You CAN override me, I'm NOT a placeholder."""
print("do something")
class AlgorithmA(Algorithm):
def do_step_1(self):
print("do step 1 for Algorithm A")
def do_step_2(self):
print("do step 2 for Algorithm A")
class AlgorithmB(Algorithm):
def do_step_1(self):
print("do step 1 for Algorithm B")
def do_step_2(self):
print("do step 2 for Algorithm B")
def do_something(self):
print("do something else")
def main():
print("Algorithm A")
a = AlgorithmA()
a.template_method()
print("\nAlgorithm B")
b = AlgorithmB()
b.template_method()
if __name__ == "__main__":
main()