added abstract factory pattern
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user