-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate_pattern.py
More file actions
51 lines (35 loc) · 1.34 KB
/
template_pattern.py
File metadata and controls
51 lines (35 loc) · 1.34 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
"""
This is part of Behavioural Design pattern. Intent is to have a abstract class as base, and a template method declared
inside with a series of calls to the different steps defined inside.
The steps may or not be abstract. Derived classes should choose the steps, and modify them if needed, but cant modify
template method in base class.
http://34.212.143.74/s201911/pycon2019/docs/design_patterns.html
..date.. March 22nd 2020
..real-time eg.. https://refactoring.guru/design-patterns/template-method
"""
from abc import ABC, abstractmethod
class BaseCls(ABC):
def templated(self):
"""
Template method which shows the directives of how the steps gonna be implemented.
:return: None
"""
self.data = self.open()
if self.data:
return self.process_data()
@abstractmethod
def open(self):
""" Step method. Can have default implementation or abstracted"""
pass
def process_data(self):
""" Step method. Can have default implementation or abstracted"""
return len(self.data)
class Derive1(BaseCls):
def __init__(self, fp):
self.file_path = fp
def open(self):
"""Overriding Step method"""
with open(self.file_path) as fp:
return fp.read()
d = Derive1('singleton_design.py')
print(d.templated())