Last active
September 1, 2016 13:38
-
-
Save alessandrocucci/84ae322441847ccdd2b332990feb1481 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
""" | |
Esempio banale di Factory Pattern. | |
Valido solo in Python 2.x | |
""" | |
from abc import ABCMeta, abstractmethod | |
class AnimaleBase(object): | |
__metaclass__ = ABCMeta | |
@abstractmethod | |
def fai_un_verso(self): | |
pass | |
class Cane(AnimaleBase): | |
def fai_un_verso(self): | |
print "Bau Bau" | |
class Gatto(AnimaleBase): | |
def fai_un_verso(self): | |
print "Miao Miao" | |
class AnimaliFactory(object): | |
def parla(self, tipo_animale): | |
return tipo_animale().fai_un_verso() | |
if __name__ == '__main__': | |
af = AnimaliFactory() | |
animale = input('Fai fare un verso a un animale: scegli tra "Gatto" e "Cane": ') | |
af.parla(animale) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment