Skip to content

Instantly share code, notes, and snippets.

@ty60
Last active November 25, 2017 04:23
Show Gist options
  • Save ty60/9f7a3195f6a9b8a41f825be5044fb3b4 to your computer and use it in GitHub Desktop.
Save ty60/9f7a3195f6a9b8a41f825be5044fb3b4 to your computer and use it in GitHub Desktop.
Abstract factory pattern
class Dog(object):
def speak(self):
return "woof"
class Cat(object):
def speak(self):
return "meow"
class Komasan(object):
def speak(self):
return "mongeee"
class DogFactory(Factory):
def get_pet(self):
return Komasan()
def make_them_speak_no_pattern():
d, c = Dog(), Cat()
print d.speak()
print c.speak()
class Factory(object):
__metaclass__ = abc.ABCMeta
@abstractmethod
def get_pet(self):
""""""
class DogFactory(Factory):
def get_pet(self):
return Dog()
class CatFactory(Factory):
def get_pet(self):
return Cat()
def make_them_speak():
d, c = DogFactory().get_pet(), CatFactory().get_pet()
print d.speak()
print c.speak()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment