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 Adapter pattern is one of the design patterns defined by GoF (Gang of Four; 4 gangs). The Adapter pattern allows you to change the interface without modifying an existing class. There are "** method using inheritance " and " method using delegation **" as a method to realize the Adapter pattern.
UML class diagram
Adapter using inheritance is realized by creating a subclass of the class you want to use and implementing the necessary interface for that subclass.
Adapter using delegation is realized by creating an instance of the class you want to use and using that instance from another class. (The above is quoted from Wikipedia)
Actually, I would like to run a sample program that utilizes the Adapter pattern and check the following behavior.
--Display character strings in parentheses --Indicate with * marks before and after the character string
$ python Main.py
(Hello)
*Hello*
Similar code has been uploaded to the Git repository. https://github.com/ttsubo/study_of_design_pattern/tree/master/Adapter
--Directory structure
.
├── Main.py
└── adapter
├── __init__.py
├── banner.py
├── print.py
└── print_banner.py
The Target
role defines the interface involved in the behavior of the instance.
In the sample program, the Print
class serves this role.
adapter/print.py
from abc import ABCMeta, abstractmethod
class Print(metaclass=ABCMeta):
@abstractmethod
def printWeak(self):
pass
@abstractmethod
def printStrng(self):
pass
It is a role to work by using the method of the role of Target
.
In the sample program, the startMain
method serves this role.
Main.py
from adapter.print_banner import PrintBanner
def startMain():
p = PrintBanner("Hello")
p.printWeak()
p.printStrng()
if __name__ == '__main__':
startMain()
Here we implement the method that actually works in the role of ʻAdapter. In the sample program, the
Banner` class serves this role.
adapter/banner.py
class Banner(object):
def __init__(self, string):
self.__string = string
def showWithParen(self):
print("({0})".format(self.__string))
def showWithAster(self):
print("*{0}*".format(self.__string))
The ʻAdapterrole is a class that implements the interface for the
Targetrole. In the sample program, the
PrintBanner` class serves this role.
There are the following two methods for realizing the Adapter pattern.
--Method using inheritance --Method using delegation
adapter/print_banner.py
from adapter.banner import Banner
from adapter.print import Print
class PrintBanner(Banner, Print):
def __init__(self, string):
super(PrintBanner, self).__init__(string)
def printWeak(self):
self.showWithParen()
def printStrng(self):
self.showWithAster()
adapter/print_banner.py
from adapter.banner import Banner
from adapter.print import Print
class PrintBanner(Print):
def __init__(self, string):
self.__banner = Banner(string)
def printWeak(self):
self.__banner.showWithParen()
def printStrng(self):
self.__banner.showWithAster()
-[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) -Adapter pattern from "diary of tachikawa844"
Recommended Posts