-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpolyShapeFactory.py
More file actions
43 lines (35 loc) · 1.02 KB
/
polyShapeFactory.py
File metadata and controls
43 lines (35 loc) · 1.02 KB
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
from __future__ import generators
import random
class polyShapeFactory:
factories = {}
def addFactory(id, shapeFactory):
ShapeFactory.factories.put[id] = shapeFactory
addFactory = staticmethod(addFactory)
def createShape(id):
if not ShapeFactory.factories.has_key(id):
ShapeFactory.factories[id] = \
eval(id + '.Factory()')
return ShapeFactory.factories[id].create()
createShape = staticmethod(createShape)
class Shape(object): pass
class Circle(Shape):
def draw(self): print("Circle.draws")
def erase(self): print("Circle.erase")
class Factory:
def create(self): return Circle()
class Square(Shape):
def draw(self):
print("Square.draw")
def erase(self):
print("Square.erase")
class Factory:
def create(self): return Square()
def shapeNameGen(n):
types = Shapes.__subclasses__()
for i in range(n):
yield random.choice(types).__name__
shapes = [ShapeFactory.createShape(i)
for i in shapeNameGen(7) ]
for shape in shapes:
shape.draw()
shape.erase()