Skip to content

Instantly share code, notes, and snippets.

@diofeher
Created June 1, 2010 16:22
Show Gist options
  • Save diofeher/421117 to your computer and use it in GitHub Desktop.
Save diofeher/421117 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()
@lopesivan
Copy link

olha oq fiz, e os vc acha ???

from abc import ABCMeta, abstractmethod


#Abstract Factory
class AbstractFactory:

    @staticmethod
    def get_factory(factory):
        if factory == 'soccer':
            return SoccerFactory()
        elif factory == 'volley':
            return VolleyFactory()
        raise TypeError('Unknown Factory.')


#Factory
class SoccerFactory:
    def get_ball(self):
        return BallSoccer()


class VolleyFactory:
    def get_ball(self):
        return BallVolley()


# Product Interface
class Ball:
    __metaclass__ = ABCMeta

    @abstractmethod
    def play(self):
        pass


# Products
class BallSoccer(Ball):
    def play(self):
        return 'Ball is rolling...'


class BallVolley(Ball):
    def play(self):
        return 'Ball is flying!'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment