Skip to content

Instantly share code, notes, and snippets.

@diofeher
Created June 1, 2010 15:37
Show Gist options
  • Save diofeher/421060 to your computer and use it in GitHub Desktop.
Save diofeher/421060 to your computer and use it in GitHub Desktop.
Factory Method implemented in Python
#!/usr/bin/python
# -*- coding : utf-8 -*-
"""
@author: Diogenes Augusto Fernandes Herminio <diofeher@gmail.com>
"""
# 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