-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.py
More file actions
executable file
·72 lines (49 loc) · 1.59 KB
/
Copy pathtemplate.py
File metadata and controls
executable file
·72 lines (49 loc) · 1.59 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
72
import sys
from abc import ABC, abstractmethod
class AbstractClass(ABC):
#This class inherit from Abstract Base Class to allow the use of the @abstractmethod decorator
def template_method(self):
"""Ths is the template method that contains a collection of
methods to stay the same, to be overriden, and to be overriden optionally.
"""
self.__always_do_this()
self.do_step_1()
self.do_step_2()
self.do_this_or()
def __always_do_this(self):
#This is a protected method that should not be overriden.
name = sys._getframe().f_code.co_name
print('{}.{}'.format(self.__class__.__name__, name))
@abstractmethod
def do_step_1(self):
#This method should be overriden
pass
@abstractmethod
def do_step_2(self):
#This method should be overriden
pass
def do_this_or(self):
print('You can overide me but you do not have to')
class ConcreteClassA(AbstractClass):
#This class inherits from the Abstract class featuring the template method.
def do_step_1(self):
print('Doing step 1 for ConcreteClassA ...')
def do_step_2(self):
print('Doing step 2 for ConcreteClassA ...')
class ConcreteClassB(AbstractClass):
#This class inherits from the Abstract class featuring the template method.
def do_step_1(self):
print('Doing step 1 for ConcreteClassB ...')
def do_step_2(self):
print('Doing step 2 for ConcreteClassB ...')
def do_this_or(self):
print('Doing my own business ...')
def main():
print('==ConcreteClassA==')
a = ConcreteClassA()
a.template_method()
print('==ConcreteClassB==')
b = ConcreteClassB()
b.template_method()
if __name__ == '__main__':
main()