Skip to content

Commit 8a9ef58

Browse files
Added prototype pattern example
1 parent ac63bfa commit 8a9ef58

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
"""
2+
Author: CHIRAG SHAH
3+
Created On: 26th July 2018
4+
"""
5+
6+
import inspect, sys, copy
7+
import matplotlib.pyplot as plt
8+
from pathlib import Path, PureWindowsPath
9+
from abc import ABCMeta, abstractmethod
10+
11+
sys.path.append(str(Path(__file__).resolve().parent.parent))
12+
from utility import class_diagram, output_image
13+
14+
class Shape(metaclass = ABCMeta):
15+
"""
16+
Abstract class
17+
"""
18+
def __init__(self):
19+
self.id = None
20+
self.type = None
21+
22+
@abstractmethod
23+
def draw(self):
24+
pass
25+
26+
def get_type(self):
27+
return self.type
28+
29+
def get_id(self):
30+
return self.id
31+
32+
def set_id(self, id):
33+
self.id = id
34+
35+
def clone(self):
36+
return copy.copy(self)
37+
38+
class Rectangle(Shape):
39+
"""
40+
Concrete class extending base class
41+
"""
42+
43+
def __init__(self):
44+
super().__init__()
45+
self.type = "Rectangle"
46+
47+
def draw(self):
48+
print("Drawing Rectangle with draw()..")
49+
50+
class Circle(Shape):
51+
"""
52+
Concrete class extending base class
53+
"""
54+
55+
def __init__(self):
56+
super().__init__()
57+
self.type = "Circle"
58+
59+
def draw(self):
60+
print("Drawing Circle with draw()..")
61+
62+
class Square(Shape):
63+
"""
64+
Concrete class extending base class
65+
"""
66+
67+
def __init__(self):
68+
super().__init__()
69+
self.type = "Square"
70+
71+
def draw(self):
72+
print("Drawing Square with draw()..")
73+
74+
class ShapeCacheAddr:
75+
"""
76+
Maps classes with their id's in dict.
77+
Returns object clones when needed
78+
"""
79+
cache = {}
80+
81+
@staticmethod
82+
def get_shape(shape_id):
83+
shape = ShapeCacheAddr.cache.get(shape_id, None)
84+
return shape.clone()
85+
86+
@staticmethod
87+
def load():
88+
circle = Circle()
89+
circle.set_id("1")
90+
ShapeCacheAddr.cache[circle.get_id()] = circle
91+
92+
square = Square()
93+
square.set_id("2")
94+
ShapeCacheAddr.cache[square.get_id()] = square
95+
96+
rectangle = Rectangle()
97+
rectangle.set_id("3")
98+
ShapeCacheAddr.cache[rectangle.get_id()] = rectangle
99+
100+
def test_prototype():
101+
"""
102+
Demonstration of prototype pattern
103+
"""
104+
105+
ShapeCacheAddr.load()
106+
circle = ShapeCacheAddr.get_shape("1")
107+
print(circle.get_type())
108+
109+
square = ShapeCacheAddr.get_shape("2")
110+
print(square.get_type())
111+
112+
rectangle = ShapeCacheAddr.get_shape("3")
113+
print(rectangle.get_type())
114+
115+
def get_code():
116+
"""
117+
@return-values: source code
118+
"""
119+
a = inspect.getsource(Shape)
120+
b = inspect.getsource(Circle)
121+
c = inspect.getsource(Square)
122+
d = inspect.getsource(Rectangle)
123+
e = inspect.getsource(ShapeCacheAddr)
124+
f = inspect.getsource(test_prototype)
125+
126+
return a + '\n' + b + '\n' + c + '\n' + d + '\n' + e + '\n' + f
127+
128+
def get_classdiagram():
129+
"""
130+
@return-values: matplotlib object with class diagram
131+
"""
132+
133+
diagram = class_diagram("prototype.png")
134+
plt.show()
135+
return diagram
136+
137+
def get_outputimage():
138+
"""
139+
@return-values: matplotlib object with code output
140+
"""
141+
142+
output = output_image("prototype_shape.png")
143+
plt.show()
144+
return output
145+
146+
test_prototype()
118 KB
Loading
2.27 KB
Loading

0 commit comments

Comments
 (0)