added external design pattern documentation links to behavioural patterns.
This commit is contained in:
@@ -1,18 +1,12 @@
|
||||
from abc import ABCMeta, abstractmethod
|
||||
|
||||
|
||||
class ChainException(Exception):
|
||||
"""
|
||||
Exception for when a chain link could not handle a request.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class ChainLink(object, metaclass=ABCMeta):
|
||||
"""
|
||||
Abstract ChainLink object as part of the Chain of Responsibility pattern.
|
||||
"""
|
||||
|
||||
External Chain of Responsibility Pattern documentation: U{https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern}
|
||||
"""
|
||||
def __init__(self):
|
||||
"""
|
||||
Initialize a new ChainLink instance.
|
||||
@@ -34,10 +28,7 @@ class ChainLink(object, metaclass=ABCMeta):
|
||||
|
||||
@param request: The request to handle.
|
||||
"""
|
||||
try:
|
||||
return self.successor.handle(request)
|
||||
except AttributeError:
|
||||
raise ChainException
|
||||
return self.successor.handle(request)
|
||||
|
||||
@abstractmethod
|
||||
def handle(self, request):
|
||||
@@ -52,8 +43,9 @@ class ChainLink(object, metaclass=ABCMeta):
|
||||
class Chain(object, metaclass=ABCMeta):
|
||||
"""
|
||||
Abstract Chain class as part of the Chain of Responsibility pattern.
|
||||
"""
|
||||
|
||||
External Chain of Responsibility Pattern documentation: U{https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern}
|
||||
"""
|
||||
def __init__(self, chainlink):
|
||||
"""
|
||||
Initialize a new Chain instance.
|
||||
@@ -70,7 +62,7 @@ class Chain(object, metaclass=ABCMeta):
|
||||
"""
|
||||
try:
|
||||
return self.chainlink.handle(request)
|
||||
except ChainException:
|
||||
except AttributeError:
|
||||
return self.fail()
|
||||
|
||||
@abstractmethod
|
||||
|
||||
@@ -18,8 +18,9 @@ class InvalidInvokerCommandException(Exception):
|
||||
class Receiver(object, metaclass=ABCMeta):
|
||||
"""
|
||||
Abstract receiver class as part of the Command pattern.
|
||||
"""
|
||||
|
||||
External Command Pattern documentation: U{https://en.wikipedia.org/wiki/Command_pattern}
|
||||
"""
|
||||
def action(self, name, *args, **kwargs):
|
||||
"""
|
||||
Delegates which method to be called for a desired action.
|
||||
@@ -38,8 +39,9 @@ class Receiver(object, metaclass=ABCMeta):
|
||||
class Command(object, metaclass=ABCMeta):
|
||||
"""
|
||||
Abstract Command class as part of the Command pattern.
|
||||
"""
|
||||
|
||||
External Command Pattern documentation: U{https://en.wikipedia.org/wiki/Command_pattern}
|
||||
"""
|
||||
def __init__(self, receiver):
|
||||
"""
|
||||
Initialize a new command instance.
|
||||
@@ -67,8 +69,9 @@ class Command(object, metaclass=ABCMeta):
|
||||
class Invoker(object, metaclass=ABCMeta):
|
||||
"""
|
||||
Abstract Invoker class as part of the Command pattern.
|
||||
"""
|
||||
|
||||
External Command Pattern documentation: U{https://en.wikipedia.org/wiki/Command_pattern}
|
||||
"""
|
||||
def __init__(self, valid_commands):
|
||||
"""
|
||||
Initialize a new Invoker instance.
|
||||
|
||||
@@ -4,6 +4,8 @@ from abc import ABCMeta, abstractmethod
|
||||
class Context(object):
|
||||
"""
|
||||
Context class as part of the Interpreter design pattern.
|
||||
|
||||
External Interpreter Pattern documentation: U{https://en.wikipedia.org/wiki/Interpreter_pattern}
|
||||
"""
|
||||
def __init__(self, in_data):
|
||||
"""
|
||||
@@ -18,6 +20,8 @@ class Context(object):
|
||||
class Expression(object, metaclass=ABCMeta):
|
||||
"""
|
||||
Abstract Expression class as part of the Interpreter design pattern.
|
||||
|
||||
External Interpreter Pattern documentation: U{https://en.wikipedia.org/wiki/Interpreter_pattern}
|
||||
"""
|
||||
@abstractmethod
|
||||
def interpret(self, context):
|
||||
@@ -33,6 +37,8 @@ class Expression(object, metaclass=ABCMeta):
|
||||
class NonTerminalExpression(Expression, metaclass=ABCMeta):
|
||||
"""
|
||||
Base class for the NonTerminalExpression class as part of the Interpreter design pattern.
|
||||
|
||||
External Interpreter Pattern documentation: U{https://en.wikipedia.org/wiki/Interpreter_pattern}
|
||||
"""
|
||||
def __init__(self, **expressions):
|
||||
"""
|
||||
@@ -47,6 +53,8 @@ class NonTerminalExpression(Expression, metaclass=ABCMeta):
|
||||
class TerminalExpression(Expression, metaclass=ABCMeta):
|
||||
"""
|
||||
Base class for the TerminalExpression class as part of the Interpreter design pattern.
|
||||
|
||||
External Interpreter Pattern documentation: U{https://en.wikipedia.org/wiki/Interpreter_pattern}
|
||||
"""
|
||||
def __init__(self, literal):
|
||||
"""
|
||||
|
||||
@@ -4,6 +4,8 @@ from abc import ABCMeta, abstractmethod
|
||||
class Iterator(object):
|
||||
"""
|
||||
An Iterator class for the Iterator design pattern.
|
||||
|
||||
External Iterator Pattern documentation: U{https://en.wikipedia.org/wiki/Iterator_pattern}
|
||||
"""
|
||||
def __init__(self, iterable):
|
||||
"""
|
||||
@@ -24,6 +26,8 @@ class Iterator(object):
|
||||
class Iterable(object, metaclass=ABCMeta):
|
||||
"""
|
||||
An abstract class representing an Iterable object as part of the Iterator design pattern.
|
||||
|
||||
External Iterator Pattern documentation: U{https://en.wikipedia.org/wiki/Iterator_pattern}
|
||||
"""
|
||||
@abstractmethod
|
||||
def __next__(self):
|
||||
|
||||
@@ -4,6 +4,8 @@ from collections import defaultdict
|
||||
class Mediator(object):
|
||||
"""
|
||||
Mediator class as part of the Mediator design pattern.
|
||||
|
||||
External Mediator Pattern documentation: U{https://en.wikipedia.org/wiki/Mediator_pattern}
|
||||
"""
|
||||
def __init__(self):
|
||||
"""
|
||||
@@ -45,4 +47,3 @@ class Mediator(object):
|
||||
self.signals[signal_name].remove(receiver)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@ from copy import deepcopy
|
||||
class Memento(object):
|
||||
"""
|
||||
Memento class as part of the Memento design pattern.
|
||||
|
||||
External Mediator Pattern documentation: U{https://en.wikipedia.org/wiki/Memento_pattern}
|
||||
"""
|
||||
def __init__(self, state):
|
||||
"""
|
||||
@@ -22,6 +24,8 @@ class Memento(object):
|
||||
class Originator(object):
|
||||
"""
|
||||
Originator base class as part of the Memento design pattern.
|
||||
|
||||
External Mediator Pattern documentation: U{https://en.wikipedia.org/wiki/Memento_pattern}
|
||||
"""
|
||||
def commit(self):
|
||||
"""
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
class Null(object):
|
||||
"""
|
||||
A Null object class as part of the Null object design pattern.
|
||||
|
||||
External Null Object Pattern documentation: U{https://en.wikipedia.org/wiki/Null_Object_pattern}
|
||||
"""
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""
|
||||
|
||||
@@ -4,6 +4,8 @@ from abc import ABCMeta, abstractmethod
|
||||
class Observer(object, metaclass=ABCMeta):
|
||||
"""
|
||||
Abstract Observer class as part of the Observer design pattern.
|
||||
|
||||
External Observer Pattern documentation: U{https://en.wikipedia.org/wiki/Observer_pattern}
|
||||
"""
|
||||
@abstractmethod
|
||||
def update(self, **state):
|
||||
@@ -15,7 +17,9 @@ class Observer(object, metaclass=ABCMeta):
|
||||
|
||||
class Observable(object):
|
||||
"""
|
||||
Base Observable class as part of the Observer design pattern
|
||||
Base Observable class as part of the Observer design pattern.
|
||||
|
||||
External Observer Pattern documentation: U{https://en.wikipedia.org/wiki/Observer_pattern}
|
||||
"""
|
||||
def __init__(self):
|
||||
"""
|
||||
|
||||
@@ -5,6 +5,8 @@ class Strategy(object):
|
||||
"""
|
||||
An abstract Strategy class.
|
||||
All strategies should inherit this class.
|
||||
|
||||
External Strategy Pattern documentation: U{https://en.wikipedia.org/wiki/Strategy_pattern}
|
||||
"""
|
||||
__metaclass__ = ABCMeta
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@ from abc import ABCMeta, abstractmethod
|
||||
class Template(object, metaclass=ABCMeta):
|
||||
"""
|
||||
Abstract Template class as part of the Template design pattern.
|
||||
|
||||
External Template Design Pattern Documentation: U{https://en.wikipedia.org/wiki/Template_method_pattern}
|
||||
"""
|
||||
@abstractmethod
|
||||
def go(self):
|
||||
|
||||
@@ -4,6 +4,8 @@ from abc import ABCMeta, abstractmethod
|
||||
class Visitor(metaclass=ABCMeta):
|
||||
"""
|
||||
Abstract Visitor class as part of the Visitor Design Pattern.
|
||||
|
||||
External Visitor Design Pattern documentation: U{https://en.wikipedia.org/wiki/Visitor_pattern}
|
||||
"""
|
||||
def visit(self, node, *args, **kwargs):
|
||||
"""
|
||||
@@ -39,6 +41,8 @@ class Visitor(metaclass=ABCMeta):
|
||||
class Visitee(object):
|
||||
"""
|
||||
A base class for objects that wish to be able to be visited by a Visitor class.
|
||||
|
||||
External Visitor Design Pattern documentation: U{https://en.wikipedia.org/wiki/Visitor_pattern}
|
||||
"""
|
||||
def accept(self, visitor):
|
||||
"""
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from unittest import TestCase
|
||||
|
||||
from pypatterns.behavioral.chain import ChainException, ChainLink, Chain
|
||||
from pypatterns.behavioral.chain import ChainLink, Chain
|
||||
|
||||
|
||||
class ChainLinkTestCase(TestCase):
|
||||
@@ -66,7 +66,7 @@ class ChainLinkTestCase(TestCase):
|
||||
@raise AssertionError: If the test fails.
|
||||
"""
|
||||
handler = self.chain_link_one_class()
|
||||
with self.assertRaises(ChainException):
|
||||
with self.assertRaises(AttributeError):
|
||||
handler.handle("foo")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user