Design patterns¶
Behavioral design patterns¶
Chain map pattern¶
Implement the Chain of Responsibility Pattern.
-
class
py314.pattern.behavioral.chain.Messenger¶ Carry the information into the strategy.
-
class
py314.pattern.behavioral.chain.Transition¶ Carry the strategy success flag.
-
done()¶ Check that the strategy transition success ends or not.
-
validate()¶ Set the strategy success to
True.
-
-
class
py314.pattern.behavioral.chain.Result¶ Carry the result data and the strategy success flag.
-
class
py314.pattern.behavioral.chain.Strategy¶ Abstract strategy.
-
class
py314.pattern.behavioral.chain.Link(strategy, child=None)¶ Manage the movement through the chain and find a successful result.
Parameters: -
perish()¶ The link does not have a child.
-
-
class
py314.pattern.behavioral.chain.Chain(strategy, *strategies)¶ Manage the movement through the chain and find a successful result.
Parameters: strategy ( Strategy) – An initial strrategy.
Lock pattern¶
-
class
py314.pattern.behavioral.lock.UnaryLock¶ Implement bit locks 0 or 1 for UNLOCKED and LOCKED respectively. Provide an acquire and release method to switch between states.
-
acquire()¶ Acquire the current lock and set the state value to
True.
-
release()¶ Release the current lock and set the state value to
False.
-
toggle()¶ Toggle the current state lock.
-
-
class
py314.pattern.behavioral.lock.MultiLock(*locks)¶ A mapping of named locks. Exceptions
KeyErrorare not silenced.Parameters: locks (list [optional]) – A list of locks names. -
states¶ dict – The current associative states array.
-
values¶ ValuesView – The current locks states.
-
acquired(key=None)¶ Check the state of the lock named key. If key is
None, check if all locks are locked and returnTruein that case.
-
acquire(key=None)¶ Acquire the lock named key or all locks if key is
None.
-
release(key=None)¶ Release the lock named key or all locks if key is
None.
-
Memento pattern¶
-
class
py314.pattern.behavioral.memento.Transaction(deep, *targets)¶ A transaction guard acting as a syntactic sugar around a memento closure.
-
class
py314.pattern.behavioral.memento.ShallowTransactional(method)¶ A
Transactionaldecorator using shallow copy.
-
class
py314.pattern.behavioral.memento.DeepTransactional(method)¶ A
Transactionaldecorator using deep copy.
Observer pattern¶
Observer pattern¶
Maintain a list of dependents and notifies them of any state changes.
-
class
py314.pattern.behavioral.observer.Subject¶ A subject to observer.
-
attach(observer, *observers)¶ Attach the given observers to the subject.
Parameters: - observer (
Observer) – An observer to attach. - *observers – Other observers to attach.
- observer (
-
detach(observer)¶ Detach an observer from the subject.
Parameters: observer ( Observer) – An observer to detach.
-
detach_from_all()¶ Detach all observer from the subject.
-
notify(modifier=None)¶ Notify the observers that the subject has been modified.
Parameters: modifier ( Observer) – Observer to ignore.
-
Publish and subscribe pattern¶
-
class
py314.pattern.behavioral.publish_subscribe.Event¶ A generic event.
-
name¶ str – The event name.
-
-
class
py314.pattern.behavioral.publish_subscribe.Service¶ A service center.
-
notify(event)¶ Add a new event to the events to publish.
Parameters: event ( Event) – Event to publish.
-
subscribe(event, subscriber)¶ Add a subscriber for an event.
Parameters: - event (
Event) – An event to subscribe to. - subscriber (
Subscriber) – A subscriber.
- event (
-
unsubscribe(event, subscriber)¶ Remove the subscribption of subscriber for the event.
Parameters: - event (
Event) – An event to unsubscribe from. - subscriber (
Subscriber) – A subscriber.
- event (
-
update()¶ Update all subscribers.
-
-
class
py314.pattern.behavioral.publish_subscribe.Publisher(service)¶ A publisher event.
Parameters: service ( Service) – A service manager.
Creational design patterns¶
Lazy Evaluation pattern¶
Lazy Evaluation Pattern¶
Delays the eval of an expr until its value is needed and avoids repeated evals.
-
class
py314.pattern.creational.lazy_evaluation.cached_property(func)¶ A property that is only computed once per instance and then replaces itself with an ordinary attribute. Deleting the attribute resets the property.
-
class
py314.pattern.creational.lazy_evaluation.lazy_property¶ A property that works with subclasses by wrapping the decorated functions of the base class.
-
class
py314.pattern.creational.lazy_evaluation.lazy_attribute(wrapped)¶ A property that caches itself to the class object.
Pool pattern¶
Pool pattern¶
This pattern is used when creating an object is costly and when this process is
frequent, yet only a few are used at a time. A Pool is responsible for
managing those usable instances by caching them. Therefore, it is possible to
skip the costly creation of an object if one is available in the pool.
A pool allows to ‘check out’ an inactive object and then to return it. If none are available, the pool creates one to provide without wait.
-
class
py314.pattern.creational.pool.ObjectPool(queue, auto_get=False)¶ Store a set of initialized objects kept ready to use.
Parameters: - queue (
queue.Queue) – A queue containing the initialized objects. - auto_get (bool [optional]) – Automatically get the next available object in the queue.
- queue (
Property pattern¶
-
class
py314.pattern.creational.property.DictProperty(attr, key=None, read_only=False)¶ Property that maps to a key in a local dict-like attribute.
Fundamental design patterns¶
Delegation pattern¶
-
class
py314.pattern.fundamental.delegation.delegate(child, attribute)¶ A descriptor based recipe that makes it possible to write shorthands that forward attribute access from one object onto another.
Parameters:
Dictionary pattern¶
-
class
py314.pattern.fundamental.dict.DictDelegator(adapter=None)¶ A dictionary delegation model.
Whenever an item from a
DictDelegatoris asked viaget(), the key argument will always be passed to adapter before searching within the internal dictionary.In other words, this provides a facade for
dictso that each call of the form D[key] is equivalent to S[adapter(key)] if D and S are respectivelydictandDictDelegatorinstances.Parameters: adapter (callable [optional]) – A key adapter. -
adapter(key)¶ Pass key to the adapter.
-
get(key, default=None)¶ Return value of self.key_adapter(key) or default.
-
pop(key, default=None)¶ Return removed value of self.key_adapter(key) or default.
-
random()¶ Return a random element.
-
Structural design patterns¶
Model View Controller pattern¶
Model View Controller Pattern¶
The MVC pattern divides an application into processing, output and input.
The processing area is represented by the ABCModel that encapsulates
core data and functionality. The model is implemented so that it is independent
of specific output representations or input behavior.
The output area is represented by the ABCView component, which is
responsible for displaying information to the user. A view obtains the data
from the model. There can be multiple views of the model.
The input area is represented by the ABCController component, which
receive input, usually as events encoding mouse movement, activation of mouse
buttons, or keyboard input.
Events are translated to service requests for the model or the view. The user interacts with the system solely through controllers.
-
class
py314.pattern.structural.mvc.ABCModel¶ Represent the functional core of the application, encapsulate appropriate data, and export procedures performing application-specific processing.
Controllers call these procedures on behalf of the user. The model also provides functions to access its data that are used by view components to acquire the data to be displayed.
The change-propagation mechanism maintains a registry of the dependent components within the model. All views and selected controllers register their need to be informed about changes. Changes to the state of the model will trigger the change-propagation mechanism, which is the only link between the model and the views and controllers.
-
publish()¶ Publish the event changes through a known handler.
-
get(*args, **kwargs)¶ Retrieve specific data from the model.
-
-
class
py314.pattern.structural.mvc.ABCView¶ Present information to the user. Different views present the information of the model in different ways. Each view defines an
update()procedure that is activated by the change-propagation mechanism.On update procedure called, a view retrieves the current data values to be displayed from the model, and puts them on the screen.
During initialization, the views are associated with a suitable model and register with the change-propagation mechanism. An
ABCControlleris created for each view accordingly. Therefore, there is an one-to-one relationship between views and controllers.Views often offer functionality that allows controllers to manipulate the display. This is useful for user-triggered operations that do not affect the model, such as scrolling.
-
update(*args, **kwargs)¶ Update the view accordingly.
-
render(*args, **kwargs)¶ Render the view accordingly.
-
-
class
py314.pattern.structural.mvc.ABCController(model, view)¶ Accept user input as events. How these events are delivered to a controller depends on the user interface platform.
For instance, let us assume that a controller implements an event-handling procedure that is called for each relevant event. Events are thereafter translated into requests for the model or the associated view.
If the behavior of a controller depends on the state of the model, then the controller registers itself with the change-propagation mechanism and also implements an update procedure. For example, this is necessary when a change to the model enables or disables a menu entry.
An
ABCControllermay ask anABCModelto update its data, or ask anABCViewto change its presentation, for instance showing a dialog instead of outputing to console. Basically, this class translates user inputs into commands to theABCVieworABCModel.Parameters: