added abstract factory pattern

This commit is contained in:
tylerlaberge
2016-07-24 16:03:55 -04:00
parent cf68b753d7
commit 6c3aedc08b
2 changed files with 91 additions and 15 deletions
+35 -2
View File
@@ -3,11 +3,10 @@ from abc import abstractmethod, ABCMeta
class Factory(object, metaclass=ABCMeta):
"""
Abstract Factory Class.
Factory Interface.
All Factories should inherit this class and overwrite the create method.
"""
@abstractmethod
def create(self, **kwargs):
"""
@@ -19,3 +18,37 @@ class Factory(object, metaclass=ABCMeta):
"""
pass
class AbstractFactory(object):
"""
Abstract Factory Class.
"""
def __init__(self):
"""
Initialize the abstract factory.
Concrete implementations should call this from within their own __init__ method
and register all their factories to keys using the register method.
"""
self._factories = dict()
@abstractmethod
def create(self, **kwargs):
"""
Abstract create method.
Concrete implementations should return a new instance of an object by calling the appropriate factory.
@param kwargs: Arguments for object creation.
"""
pass
def _register(self, key, factory):
"""
Register a factory to a key.
@param key: Key for identifying which factory to use.
@type key: str
@param factory: The factory to register to the key.
"""
self._factories[str(key)] = factory()