Skip to content

Instantly share code, notes, and snippets.

@creativ

creativ/factory_v3

Last active Jan 3, 2016
Embed
What would you like to do?
Simple factories
def shape_factory(type, *args, **kwargs):
if type == "Circle": return Circle(*args, **kwargs)
elif type == "Square": return Square(*args, **kwargs)
else: None
class Shape(object):
def draw(self):
raise NotImplementedError()
def erase(self):
raise NotImplementedError()
class Circle(Shape):
def draw(self):
print("Circle.draw")
def erase(self):
print("Circle.erase")
class Square(Shape):
def draw(self):
print("Square.draw")
def erase(self):
print("Square.erase")
circle = shape_factory('Circle')
circle.draw()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.