-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflyweight_pattern.py
43 lines (32 loc) · 1002 Bytes
/
flyweight_pattern.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Shape:
def draw(self):
pass
class Circle(Shape):
def __init__(self, color: 'str'):
self.x = 0
self.y = 0
self.color = color
def set_x(self, x):
self.x = x
def set_y(self, y):
self.y = y
def draw(self):
print("Shape : Circle [" + str(self.x) + ',' + str(self.y) + '] color: ' + self.color)
class ShapeFactory:
def __init__(self):
self.circle_map = dict()
def getCircle(self, color: 'str'):
circle = self.circle_map.get(color)
if circle is None:
circle = Circle(color)
self.circle_map[color] = circle
print("creating circle of color: " + color)
return circle
import random
shape_factory = ShapeFactory()
colors = ['Red', 'Green', 'Blue', 'White', 'Black']
for x in range(1, 20):
circle = shape_factory.getCircle(random.choice(colors))
circle.set_x(random.randint(1, 5))
circle.set_y(random.randint(6, 9))
circle.draw()