renamed package.
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
from abc import ABCMeta, abstractmethod
|
||||
|
||||
|
||||
class Director(object, metaclass=ABCMeta):
|
||||
"""
|
||||
Abstract director class, responsible for using a builder to fully construct an object.
|
||||
|
||||
- External Usage documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Creational-Pattern-Usage}
|
||||
- External Builder Pattern documentation: U{https://en.wikipedia.org/wiki/Builder_pattern}
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
Initialize a new Director.
|
||||
"""
|
||||
self.builder = None
|
||||
|
||||
@abstractmethod
|
||||
def construct(self):
|
||||
"""
|
||||
Abstract method for fully constructing an object.
|
||||
|
||||
Concrete implementations should override this and use a builder to construct the object.
|
||||
|
||||
@raise NotImplementedError: If this method is not overridden.
|
||||
"""
|
||||
pass
|
||||
|
||||
def get_constructed_object(self):
|
||||
"""
|
||||
Get the object this director is responsible for constructing.
|
||||
|
||||
@return: The object that this director is responsible for constructing.
|
||||
"""
|
||||
return self.builder.constructed_object
|
||||
|
||||
|
||||
class Builder(object, metaclass=ABCMeta):
|
||||
"""
|
||||
Abstract builder class, responsible for constructing various pieces of an object.
|
||||
|
||||
- External Usage documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Creational-Pattern-Usage}
|
||||
- External Builder Pattern documentation: U{https://en.wikipedia.org/wiki/Builder_pattern}
|
||||
"""
|
||||
|
||||
def __init__(self, constructed_object):
|
||||
"""
|
||||
Initialize a new Builder.
|
||||
|
||||
Concrete Builders should call this method from within their own __init__ method.
|
||||
The concrete __init__ method should also register all build options to build methods,
|
||||
by using the _register method.
|
||||
|
||||
@param constructed_object: An instance of an object this builder is responsible for.
|
||||
"""
|
||||
self.constructed_object = constructed_object
|
||||
self.build_methods = dict()
|
||||
|
||||
def build(self, build_option, **kwargs):
|
||||
"""
|
||||
Build a piece of the constructed object.
|
||||
|
||||
@param build_option: The part of the object to build. All build options should have been registered in __init__.
|
||||
@type build_option: str
|
||||
@param kwargs: Additional arguments for building.
|
||||
"""
|
||||
self.build_methods[build_option](**kwargs)
|
||||
|
||||
def _register(self, build_option, build_method):
|
||||
"""
|
||||
Register a build option to a build method.
|
||||
|
||||
All concrete builders should call this method in their constructor at least once.
|
||||
|
||||
@param build_option: A string representing the part of the object to build.
|
||||
@type build_option: str
|
||||
@param build_method: The method to call when given build option is selected.
|
||||
"""
|
||||
self.build_methods[build_option] = build_method
|
||||
@@ -0,0 +1,60 @@
|
||||
from abc import abstractmethod, ABCMeta
|
||||
|
||||
|
||||
class Factory(object, metaclass=ABCMeta):
|
||||
"""
|
||||
Factory Interface.
|
||||
|
||||
All Factories should inherit this class and overwrite the create method.
|
||||
|
||||
- External Usage documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Creational-Pattern-Usage}
|
||||
- External Factory Pattern documentation: U{https://en.wikipedia.org/wiki/Factory_method_pattern}
|
||||
"""
|
||||
@abstractmethod
|
||||
def create(self, **kwargs):
|
||||
"""
|
||||
Abstract create method.
|
||||
|
||||
Concrete implementations should return a new instance of the object the factory class is responsible for.
|
||||
@param kwargs: Arguments for object creation.
|
||||
@return: A new instance of the object the factory is responsible for.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class AbstractFactory(Factory, metaclass=ABCMeta):
|
||||
"""
|
||||
Abstract Factory Class as part of the AbstractFactory design pattern.
|
||||
|
||||
- External Usage documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Creational-Pattern-Usage}
|
||||
- External Abstract Factory Pattern documentation: U{https://en.wikipedia.org/wiki/Abstract_factory_pattern}
|
||||
"""
|
||||
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
|
||||
@@ -0,0 +1,73 @@
|
||||
from abc import ABCMeta
|
||||
from copy import deepcopy
|
||||
from pypat.behavioral.memento import Originator
|
||||
|
||||
|
||||
class Reusable(Originator, metaclass=ABCMeta):
|
||||
"""
|
||||
An abstract reusable class.
|
||||
|
||||
External Object Pool Pattern documentation: U{https://en.wikipedia.org/wiki/Object_pool_pattern}
|
||||
"""
|
||||
def __init__(self):
|
||||
"""
|
||||
Initialize a new Reusable instance.
|
||||
"""
|
||||
self.memento = self.commit()
|
||||
|
||||
def reset(self):
|
||||
"""
|
||||
Reset this objects state to the state that it was created with.
|
||||
"""
|
||||
memento = self.memento
|
||||
self.rollback(deepcopy(memento))
|
||||
self.memento = memento
|
||||
|
||||
|
||||
class Pool(object):
|
||||
"""
|
||||
An Object Pool design pattern implementation.
|
||||
|
||||
External Object Pool Pattern documentation: U{https://en.wikipedia.org/wiki/Object_pool_pattern}
|
||||
"""
|
||||
def __init__(self, reusable_class, *args, **kwargs):
|
||||
"""
|
||||
Initialize a new object pool instance.
|
||||
|
||||
@param reusable_class: The reusable class this object pool is responsible for.
|
||||
@param args: args for reusable object creation.
|
||||
@param kwargs: kwargs for reusable object creation.
|
||||
"""
|
||||
self.reusables = list()
|
||||
self.reusable_class = reusable_class
|
||||
self.args = args
|
||||
self.kwargs = kwargs
|
||||
self.pool_size = 2
|
||||
self._expand(self.pool_size)
|
||||
|
||||
def acquire(self):
|
||||
"""
|
||||
Acquire an object from the pool.
|
||||
|
||||
@return: An object from the pool.
|
||||
"""
|
||||
try:
|
||||
reusable = self.reusables.pop()
|
||||
except IndexError:
|
||||
self._expand(self.pool_size)
|
||||
reusable = self.reusables.pop()
|
||||
|
||||
return reusable
|
||||
|
||||
def release(self, reusable):
|
||||
"""
|
||||
Release an object back into the pool.
|
||||
|
||||
@param reusable: The object to return to the pool.
|
||||
"""
|
||||
reusable.reset()
|
||||
self.reusables.append(reusable)
|
||||
|
||||
def _expand(self, size):
|
||||
for i in range(0, size):
|
||||
self.reusables.append(self.reusable_class(*self.args, **self.kwargs))
|
||||
@@ -0,0 +1,25 @@
|
||||
from copy import deepcopy
|
||||
from types import MethodType
|
||||
|
||||
|
||||
class Prototype(object):
|
||||
"""
|
||||
Prototype design pattern abstract class.
|
||||
|
||||
External Prototype Pattern documentation: U{https://en.wikipedia.org/wiki/Prototype_pattern}
|
||||
"""
|
||||
def prototype(self, **attributes):
|
||||
"""
|
||||
Copy the prototype this object and optionally update attributes.
|
||||
|
||||
@param attributes: Keyword arguments of any attributes you wish to update.
|
||||
@return: A copy of this object with the updated attributes.
|
||||
"""
|
||||
obj = deepcopy(self)
|
||||
for attribute in attributes:
|
||||
if callable(attributes[attribute]):
|
||||
setattr(obj, attribute, MethodType(attributes[attribute], obj))
|
||||
else:
|
||||
setattr(obj, attribute, attributes[attribute])
|
||||
|
||||
return obj
|
||||
@@ -0,0 +1,18 @@
|
||||
class Singleton(type):
|
||||
"""
|
||||
Singleton Metaclass.
|
||||
|
||||
Enforces any object using this metaclass to only create a single instance.
|
||||
|
||||
External Singleton Pattern documentation: U{https://en.wikipedia.org/wiki/Singleton_pattern}
|
||||
"""
|
||||
__instance = None
|
||||
|
||||
def __call__(cls, *args, **kwargs):
|
||||
"""
|
||||
Override the __call__ method to make sure only one instance is created.
|
||||
"""
|
||||
if cls.__instance is None:
|
||||
cls.__instance = type.__call__(cls, *args, **kwargs)
|
||||
|
||||
return cls.__instance
|
||||
Reference in New Issue
Block a user