Skip to content

Instantly share code, notes, and snippets.

@supernullset
Created November 24, 2014 17:16
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 supernullset/c772c449d5177735dcd7 to your computer and use it in GitHub Desktop.
Save supernullset/c772c449d5177735dcd7 to your computer and use it in GitHub Desktop.
# 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