-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory.py
More file actions
47 lines (38 loc) · 977 Bytes
/
factory.py
File metadata and controls
47 lines (38 loc) · 977 Bytes
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
44
45
46
47
"""
Factory Pattern
"""
from abc import ABCMeta,abstractmethod
class Shape(metaclass = ABCMeta):
@abstractmethod
def draw():
pass
class Rectangle(Shape):
def draw(self):
print("make a rectanle object")
class Square(Shape):
def draw(self):
print("make a square object")
class Circle(Shape):
def draw(self):
print("make a circle object")
class ShapeFactory():
def __init__(self, name):
self.name = name
def GetObject(self):
return self.__makeShapeObj()
def __makeShapeObj(self):
if self.name == "rectangle":
return Rectangle()
if self.name == "square":
return Square()
if self.name == "circle":
return Circle()
print("unknow object:%s"%(self.name))
return None
if __name__ == "__main__":
obj1 = ShapeFactory("rectangle")
obj1.GetObject().draw()
obj2 = ShapeFactory("square")
obj2.GetObject().draw()
obj3 = ShapeFactory("circle")
obj3.GetObject().draw()