Package pypat :: Package creational :: Module factory
[hide private]
[frames] | no frames]

Source Code for Module pypat.creational.factory

 1  from abc import abstractmethod, ABCMeta 
2 3 4 -class Factory(object, metaclass=ABCMeta):
5 """ 6 Factory Interface. 7 8 All Factories should inherit this class and overwrite the create method. 9 10 - External Usage documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Creational-Pattern-Usage} 11 - External Factory Pattern documentation: U{https://en.wikipedia.org/wiki/Factory_method_pattern} 12 """ 13 @abstractmethod
14 - def create(self, **kwargs):
15 """ 16 Abstract create method. 17 18 Concrete implementations should return a new instance of the object the factory class is responsible for. 19 @param kwargs: Arguments for object creation. 20 @return: A new instance of the object the factory is responsible for. 21 """ 22 pass
23
24 25 -class AbstractFactory(Factory, metaclass=ABCMeta):
26 """ 27 Abstract Factory Class as part of the AbstractFactory design pattern. 28 29 - External Usage documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Creational-Pattern-Usage} 30 - External Abstract Factory Pattern documentation: U{https://en.wikipedia.org/wiki/Abstract_factory_pattern} 31 """
32 - def __init__(self):
33 """ 34 Initialize the abstract factory. 35 36 Concrete implementations should call this from within their own __init__ method 37 and register all their factories to keys using the register method. 38 """ 39 self._factories = dict()
40 41 @abstractmethod
42 - def create(self, **kwargs):
43 """ 44 Abstract create method. 45 46 Concrete implementations should return a new instance of an object by calling the appropriate factory. 47 48 @param kwargs: Arguments for object creation. 49 """ 50 pass
51
52 - def _register(self, key, factory):
53 """ 54 Register a factory to a key. 55 56 @param key: Key for identifying which factory to use. 57 @type key: str 58 @param factory: The factory to register to the key. 59 """ 60 self._factories[str(key)] = factory
61