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 Flyweight pattern is one of the design patterns defined by GoF. The purpose is to save resources in the program by reusing one instance when using equivalent instances in different places.
UML class and sequence diagram UML class diagram (The above is quoted from Wikipedia)
** flyweight ** stands for "flyweight", which is the lightest weight class in boxing, and in this design pattern, it is said to be for "lightening" objects.
The technique used in the Flyweight
pattern is to" share instances as much as possible and not wastefully new ", so when you need an instance, you can use the already created instance instead of always newing it. Then, it seems that it is shared and used.
Intuitively, I feel that it is an application of design pattern "Singleton".
Prepare a text file that displays numbers like Akky Art.
big0.txt
....######......
..##......##....
..##......##....
..##......##....
..##......##....
..##......##....
....######......
................
big1.txt
......##........
..######........
......##........
......##........
......##........
......##........
..##########....
................
big2.txt
....######......
..##......##....
..........##....
......####......
....##..........
..##............
..##########....
................
(Hereafter omitted)
I would like to actually run a sample program that utilizes the Flyweight pattern and check the following behavior.
--Display the specified numbers in ASCII art in the order of the argument numbers
$ python Main.py 012123
....######......
..##......##....
..##......##....
..##......##....
..##......##....
..##......##....
....######......
................
......##........
..######........
......##........
......##........
......##........
......##........
..##########....
................
....######......
..##......##....
..........##....
......####......
....##..........
..##............
..##########....
................
......##........
..######........
......##........
......##........
......##........
......##........
..##########....
................
....######......
..##......##....
..........##....
......####......
....##..........
..##............
..##########....
................
....######......
..##......##....
..........##....
......####......
..........##....
..##......##....
....######......
................
Similar code has been uploaded to the Git repository. https://github.com/ttsubo/study_of_design_pattern/tree/master/Flyweight
--Directory structure
.
├── Main.py
├── big0.txt
├── big1.txt
├── big2.txt
├── big3.txt
├── big4.txt
├── big5.txt
├── big6.txt
├── big7.txt
├── big8.txt
├── big9.txt
└── flyweight
├── __init__.py
└── big_char_factory.py
If you handle it normally, the program will be heavy, so it is a role that represents something that should be shared.
In the sample program, the BigChar
class serves this role.
flyweight/big_char_factory.py
class BigChar(object):
def __init__(self, charname):
try:
with open("big{0}.txt".format(charname), 'r') as txtfile:
data = txtfile.read()
self.__fontdata = data
except IOError:
self.__fontdata = charname + '?'
def __str__(self):
return self.__fontdata
It is the role of the factory that makes Flyweight
. If you use this factory to create a Flyweight
role, the instance will be shared.
In the sample program, the BigCharFactory
class serves this role.
flyweight/big_char_factory.py
class BigCharFactory(object):
def __init__(self):
self.__pool = {}
@classmethod
def getInstance(cls):
if not hasattr(cls, "_instance"):
cls._instance = cls()
return cls._instance
def getBigChar(self, charname):
bc = self.__pool.get(charname)
if bc is None:
bc = BigChar(charname)
self.__pool[charname] = bc
return bc
It is a role to create a Flyweight
using the Flyweight Factory
role and use it.
In the sample program, the BigString
class and the startMain
method serve this role.
flyweight/big_char_factory.py
class BigString(object):
def __init__(self, string):
self.bigchars = []
self.factory = BigCharFactory.getInstance()
for s in string:
self.bigchars.append(self.factory.getBigChar(s))
def print(self):
for bc in self.bigchars:
print(bc)
Main.py
import sys
from flyweight.big_char_factory import BigCharFactory, BigString
def startMain(string):
bs = BigString(string)
bs.print()
if __name__ == '__main__':
startMain(sys.argv[1])
-[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