Created
June 1, 2010 15:37
-
-
Save diofeher/421060 to your computer and use it in GitHub Desktop.
Factory Method implemented in Python
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
#!/usr/bin/python | |
# -*- coding : utf-8 -*- | |
""" | |
@author: Diogenes Augusto Fernandes Herminio <[email protected]> | |
""" | |
# Product | |
class Churrasco(object): | |
def __init__(self): | |
self.fala = None | |
class ChurrascoGato(Churrasco): | |
def __init__(self): | |
self.fala = 'Miau' | |
class ChurrascoCarneiro(Churrasco): | |
def __init__(self): | |
self.fala = 'Beeeeeeeh' | |
# Factory | |
class ChurrascoFactory(object): | |
@staticmethod | |
def fazer_churrasco(churras): | |
if churras == 'carneiro': | |
return ChurrascoCarneiro() | |
elif churras == 'gato': | |
return ChurrascoGato() | |
raise TypeError('Esse churrasco nao existe.') | |
#Client | |
if __name__=="__main__": | |
for i in ['gato', 'carneiro', 'cachorro']: | |
churras_obj = ChurrascoFactory.fazer_churrasco(i) | |
print churras_obj.fala |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment