Last active
January 3, 2016 13:49
-
-
Save creativ/8471801 to your computer and use it in GitHub Desktop.
Simple factories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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