Skip to content

Instantly share code, notes, and snippets.

@fercreek
Forked from diofeher/abstract_factory
Created October 15, 2015 00:45
Show Gist options
  • Save fercreek/bb3cae0a8827a23e10f6 to your computer and use it in GitHub Desktop.
Save fercreek/bb3cae0a8827a23e10f6 to your computer and use it in GitHub Desktop.
Abstract Factory Pattern implemented in Python
#!/usr/bin/python
# -*- coding : utf-8 -*-
"""
@author: Diogenes Augusto Fernandes Herminio <diofeher@gmail.com>
"""
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