Skip to content

Instantly share code, notes, and snippets.

@alessandrocucci
Last active September 1, 2016 13:38
Show Gist options
  • Save alessandrocucci/84ae322441847ccdd2b332990feb1481 to your computer and use it in GitHub Desktop.
Save alessandrocucci/84ae322441847ccdd2b332990feb1481 to your computer and use it in GitHub Desktop.
"""
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