Last active
November 25, 2017 04:23
-
-
Save ty60/9f7a3195f6a9b8a41f825be5044fb3b4 to your computer and use it in GitHub Desktop.
Abstract factory pattern
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
class Dog(object): | |
def speak(self): | |
return "woof" | |
class Cat(object): | |
def speak(self): | |
return "meow" | |
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
class Komasan(object): | |
def speak(self): | |
return "mongeee" | |
class DogFactory(Factory): | |
def get_pet(self): | |
return Komasan() |
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 make_them_speak_no_pattern(): | |
d, c = Dog(), Cat() | |
print d.speak() | |
print c.speak() |
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
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