Skip to content

Instantly share code, notes, and snippets.

@creativ
Last active January 3, 2016 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creativ/8471801 to your computer and use it in GitHub Desktop.
Save creativ/8471801 to your computer and use it in GitHub Desktop.
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