Created
November 24, 2014 17:16
-
-
Save supernullset/c772c449d5177735dcd7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# This is ripe for some refactoring, but I think it shows an okay | |
# implementation of the factory pattern | |
class Animal(object): | |
sound = "" | |
description = "" | |
def describe(self): | |
return self.description | |
class Dog(Animal): | |
sound = "bark" | |
description = "A Dog %ss" % sound | |
class Bird(Animal): | |
sound = "tweet" | |
description = "A bird %ss" % sound | |
class Programmer(Animal): | |
sound = "tap tap" | |
description = "A programmer %ss" % sound | |
class AnimalFactory(): | |
def build(self, typ): | |
tc = typ.capitalize() | |
# WOO GLOBALS | |
return globals()[tc]() | |
animal_factory = AnimalFactory() | |
animals = ['dog', 'bird', 'programmer'] | |
for a in animals: | |
print animal_factory.build(a).describe() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment