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 Prototype pattern (prototype pattern) is one of the design patterns related to generation used in software development. Used when the type of object created is a prototype (typical) instance, duplicating this prototype to create a new object.
This pattern is used for the following purposes.
--Avoid subclassing object creators in client applications, as is done in the Abstract Factory pattern --Avoid when the inherent cost of creating a new object in the standard way (eg'new') is too high for a given application
To implement this pattern, declare an abstract base class that specifies the clone () method of the pure virtual method. All classes that require the ability of "polymorphic constructors" derive themselves from abstract base classes and implement clone () operations.
UML class and sequence diagram UML class diagram
(The above is quoted from Wikipedia)
The Prototype
pattern is to create another instance from an instance by copying the instance instead of creating an instance from the class.
The benefits of using the Prototype
pattern are described on page 66 of the book [Introduction to Design Patterns Learned in the Augmented and Revised Java Language".
(1) When there are too many types to put together in a class
(2) When it is difficult to create an instance from a class
(3) When you want to separate the framework and the instance to be generated
Well, if there are use cases that generate a large number of instances, will it lead to reduction of overhead processing related to instance creation?
I would like to actually run a sample program that utilizes the Prototype pattern and check the following behavior.
--Display the character string underlined --Display the character string surrounded by a border
$ python Main.py
"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/Prototype
--Directory structure
.
├── Main.py
└── framework
├── __init__.py
├── decoprototype
│ ├── __init__.py
│ ├── message_box_prototype.py
│ └── underline_pen_prototype.py
├── manager.py
└── prototype.py
The Prototype
role defines the interface for copying (duplicate) an instance to create a new instance.
In the sample program, the Prototype
class serves this role.
framework/prototype.py
from abc import ABCMeta, abstractmethod
class Prototype(metaclass=ABCMeta):
@abstractmethod
def use(self, s):
pass
@abstractmethod
def createClone(self):
pass
The ConcretePrototype
role actually implements the method of copying an instance and creating a new one.
In the sample program, the ʻUnderlinePen class and the
MessageBox` class serve this role.
framework/decoprototype/underline_pen_prototype.py
import copy
from framework.prototype import Prototype
class UnderlinePen(Prototype):
def __init__(self, ulchar):
self.__ulchar = ulchar
def use(self, s):
length = len(s)
line = self.__ulchar * (length + 2)
print("\"{0}\"".format(s))
print("{0}\n".format(line))
def createClone(self):
clone = copy.deepcopy(self)
return clone
framework/decoprototype/message_box_prototype.py
import copy
from framework.prototype import Prototype
class MessageBox(Prototype):
def __init__(self, decochar):
self.__decochar = decochar
def use(self, s):
length = len(s)
line = self.__decochar * (length + 4)
print("{0}".format(line))
print("{0} {1} {2}".format(self.__decochar, s, self.__decochar))
print("{0}\n".format(line))
def createClone(self):
clone = copy.deepcopy(self)
return clone
The Client
role creates a new instance using the method that copies the instance.
In the sample program, the Manager
class and the startMain
method serve this role.
framework/manager.py
class Manager(object):
def __init__(self):
self.__showcase = {}
def register(self, name, proto):
self.__showcase[name] = proto
def create(self, protoname):
p = self.__showcase[protoname]
return p.createClone()
Main.py
from framework.manager import Manager
from framework.decoprototype.underline_pen_prototype import UnderlinePen
from framework.decoprototype.message_box_prototype import MessageBox
def startMain(managerObject):
upen = UnderlinePen("-")
mbox = MessageBox("*")
sbox = MessageBox("/")
managerObject.register("strong message", upen)
managerObject.register("warning box", mbox)
managerObject.register("slash box", sbox)
p1 = managerObject.create("strong message")
p2 = managerObject.create("warning box")
p3 = managerObject.create("slash box")
p1.use("Hello World")
p2.use("Hello World")
p3.use("Hello World")
if __name__ == "__main__":
startMain(Manager())
-[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) -Prototype pattern from "diary of tachikawa844"
Recommended Posts