-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathtemplate_concept.py
74 lines (59 loc) · 1.97 KB
/
template_concept.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
# pylint: disable=too-few-public-methods
"The Template Method Pattern Concept"
from abc import ABCMeta, abstractmethod
class AbstractClass(metaclass=ABCMeta):
"A template class containing a template method and primitive methods"
@staticmethod
def step_one():
"""
Hooks are normally empty in the abstract class. The
implementing class can optionally override providing a custom
implementation
"""
@staticmethod
@abstractmethod
def step_two():
"""
An abstract method that must be overridden in the implementing
class. It has been given `@abstractmethod` decorator so that
pylint shows the error.
"""
@staticmethod
def step_three():
"""
Hooks can also contain default behavior and can be optionally
overridden
"""
print("Step Three is a hook that prints this line by default.")
@classmethod
def template_method(cls):
"""
This is the template method that the subclass will call.
The subclass (implementing class) doesn't need to override this
method since it has would have already optionally overridden
the following methods with its own implementations
"""
cls.step_one()
cls.step_two()
cls.step_three()
class ConcreteClassA(AbstractClass):
"A concrete class that only overrides step two"
@staticmethod
def step_two():
print("Class_A : Step Two (overridden)")
class ConcreteClassB(AbstractClass):
"A concrete class that only overrides steps one, two and three"
@staticmethod
def step_one():
print("Class_B : Step One (overridden)")
@staticmethod
def step_two():
print("Class_B : Step Two. (overridden)")
@staticmethod
def step_three():
print("Class_B : Step Three. (overridden)")
# The Client
CLASS_A = ConcreteClassA()
CLASS_A.template_method()
CLASS_B = ConcreteClassB()
CLASS_B.template_method()