Learn the design pattern "Prototype" in Python

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.

■ Prototype (prototype pattern)

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 W3sDesign_Prototype_Design_Pattern_UML.jpg UML class diagram https---qiita-image-store.s3.ap-northeast-1.amazonaws.com-0-103595-3c90ab89-e715-587d-10eb-65490cb5be6c.png

(The above is quoted from Wikipedia)

□ Memorandum

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?

■ "Prototype" sample program

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 /
///////////////

■ Details of sample program

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

(1) The role of Prototype

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

(2) The role of Concrete Prototype

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

(3) The role of Client (user)

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())

■ Reference URL

-[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

Learn the design pattern "Prototype" in Python
Learn the design pattern "Builder" in Python
Learn the design pattern "Flyweight" in Python
Learn the design pattern "Observer" in Python
Learn the design pattern "Memento" in Python
Learn the design pattern "Proxy" in Python
Learn the design pattern "Command" in Python
Learn the design pattern "Visitor" in Python
Learn the design pattern "Bridge" in Python
Learn the design pattern "Mediator" in Python
Learn the design pattern "Decorator" in Python
Learn the design pattern "Iterator" in Python
Learn the design pattern "Strategy" in Python
Learn the design pattern "Composite" in Python
Learn the design pattern "State" in Python
Learn the design pattern "Adapter" in Python
Learn the design pattern "Abstract Factory" in Python
Learn the design pattern "Template Method" in Python
Learn the design pattern "Factory Method" in Python
Learn the design pattern "Chain of Responsibility" in Python
Learn the design pattern "Singleton" with Python
Learn the design pattern "Facade" with Python
Implement the Singleton pattern in Python
Singleton pattern in Python
Visitor pattern in Python
Prototype pattern in Java
I wrote a design pattern in kotlin Prototype
Download the file in Python
Find the difference in Python
Learn cumulative sum in Python
Design Patterns in Python: Introduction
Learn exploration in Python # 1 Full exploration
Python Design Pattern --Template method
Getting the arXiv API in Python
Python in the browser: Brython's recommendation
Save the binary file in Python
Hit the Sesami API in Python
Get the desktop path in Python
Get the script path in Python
In the python command python points to python3.8
Hit the web API in Python
I wrote the queue in Python
Calculate the previous month in Python
Examine the object's class in python
Get the desktop path in Python
Get the host name in Python
Access the Twitter API in Python
The first step in Python Matplotlib
I wrote the stack in Python
Master the weakref module in Python
Learn the basics of Python ① Beginners
Load the remote Python SDK in IntelliJ
Try using the Wunderlist API in Python
Check the behavior of destructor in Python
[Python Kivy] About changing the design theme
Try using the Kraken API in Python
Learn the basics while touching python Variables
Write the test in a python docstring
OR the List in Python (zip function)
GoF design pattern from the problem 2. Structure
Display Python 3 in the browser with MAMP