added external design pattern documentation links to behavioural patterns.

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