Created
June 1, 2010 16:22
-
-
Save diofeher/421117 to your computer and use it in GitHub Desktop.
Abstract Factory Pattern implemented in Python
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
#!/usr/bin/python | |
# -*- coding : utf-8 -*- | |
""" | |
@author: Diogenes Augusto Fernandes Herminio <[email protected]> | |
""" | |
from abc import ABCMeta | |
#Abstract Factory | |
class StandardFactory(object): | |
@staticmethod | |
def get_factory(factory): | |
if factory == 'soccer': | |
return SoccerFactory() | |
elif factory == 'volley': | |
return VolleyFactory() | |
raise TypeError('Unknown Factory.') | |
#Factory | |
class SoccerFactory(object): | |
def get_ball(self): | |
return BallSoccer(); | |
class VolleyFactory(object): | |
def get_ball(self): | |
return BallVolley(); | |
# Product Interface | |
class Ball(object): | |
__metaclass__ = ABCMeta | |
def play(self): | |
pass | |
# Products | |
class BallSoccer(object): | |
def play(self): | |
return 'Ball is rolling...' | |
class BallVolley(object): | |
def play(self): | |
return 'Ball is flying!' | |
if __name__ =="__main__": | |
factory = StandardFactory.get_factory('volley') | |
ball = factory.get_ball() | |
print ball.play() | |
factory = StandardFactory.get_factory('soccer') | |
ball = factory.get_ball() | |
print ball.play() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
olha oq fiz, e os vc acha ???