-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtemplate.py
53 lines (34 loc) · 1.28 KB
/
template.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
# ------------------------------
# Template Design Pattern.
# ------------------------------
# Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template method lets subclasses
# redefine certain steps of an algorithm without changing the algorithm's structure
from abc import ABC, abstractmethod
class Template(ABC):
def __init__(self):
pass
@abstractmethod
def function1(self):
pass
@abstractmethod
def function2(self):
pass
def execute(self):
print("Run function1 and function2.")
self.function1()
self.function2()
class TemplateImplementation1(Template):
def function1(self):
print("TemplateImplementation1.function1() called.")
def function2(self):
print("TemplateImplementation1.function2() called.")
class TemplateImplementation2(Template):
def function1(self):
print("TemplateImplementation2.function1() called.")
def function2(self):
print("TemplateImplementation2.function2() called.")
template_implementation11 = TemplateImplementation1()
template_implementation11.execute()
print("-----------------------------------------------")
template_implementation12 = TemplateImplementation2()
template_implementation12.execute()