As a material for learning GoF design patterns, the book "Introduction to Design Patterns Learned in the Augmented and Revised Java Language" seems to be helpful. However, since the examples taken up are based on JAVA, I tried the same practice in Python to deepen my understanding.
The Template Method pattern is one of the design patterns defined by GoF (Gang of Four; 4 gangs). It belongs to "behavioral patterns". The purpose of the Template Method pattern is to predetermine a rough algorithm for a process and leave the specific design of that algorithm to a subclass. Therefore, it is often used as a means for building a system framework.
UML class diagram (The above is quoted from Wikipedia)
Actually, I would like to run a sample program that utilizes the Template Method pattern and check the following behavior.
--Display the letter "H" five times in a row. In addition, before and after, it is displayed surrounded by "<<" and ">>". --Display the character string "Hello, World!" 5 times in a row. In addition, it is displayed surrounded by a frame.
$ python Main.py
<<HHHHH>>
+-------------+
|Hello, World!|
|Hello, World!|
|Hello, World!|
|Hello, World!|
|Hello, World!|
+-------------+
Similar code has been uploaded to the Git repository. https://github.com/ttsubo/study_of_design_pattern/tree/master/TemplateMethod
--Directory structure
.
├── Main.py
└── templatemethod
├── __init__.py
└── display.py
The ʻAbstractClassrole implements the template method. Also, declare the abstract method used in the template method. This abstract method is implemented by the subclass
ConcreteClass role. In the sample program, the ʻAbstractDisplay
class serves this role.
templatemethod/display.py
from abc import ABCMeta, abstractmethod
class AbstractDisplay(metaclass=ABCMeta):
@abstractmethod
def print(self):
pass
@abstractmethod
def open(self):
pass
@abstractmethod
def close(self):
pass
def display(self):
self.open()
for _ in range(5):
self.print()
self.close()
ʻConcretely implement the abstract class defined by the role of AbstractClass. The method implemented here is called from the template method that plays the role of ʻAbstractClass
.
In the sample program, the CharDisplay
and StringDisplay
classes serve this role.
templatemethod/display.py
class CharDisplay(AbstractDisplay):
def __init__(self, ch):
self.__ch = ch
def open(self):
print('<<', end='')
def print(self):
print(self.__ch, end='')
def close(self):
print('>>')
class StringDisplay(AbstractDisplay):
def __init__(self, string):
self.__string = string
self.__width = len(string)
def open(self):
self.__printLine()
def print(self):
print("|{0}|".format(self.__string))
def close(self):
self.__printLine()
def __printLine(self):
print('+', end='')
for _ in range(self.__width):
print('-', end='')
print('+')
In the sample program, the startMain
method serves this role.
Main.py
from templatemethod.display import CharDisplay, StringDisplay
def startMain():
d1 = CharDisplay('H')
d2 = StringDisplay("Hello, World!")
d1.display()
print("")
d2.display()
if __name__ == '__main__':
startMain()
-[Finishing "Introduction to Design Patterns Learned in Java Language" (Not)](https://medium.com/since-i-want-to-start-blog-that-looks-like-men-do/java Introduction to Design Patterns Learned in Language-Finishing-Not-2cc9b34a30b2)
Recommended Posts