execute read-the-docs.sh

This commit is contained in:
Faust
2015-04-02 00:03:33 +02:00
parent c6cc7f2131
commit bba8b0df43
40 changed files with 2491 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
Chain Of Responsibilities
=========================
Purpose:
--------
To build a chain of objects to handle a call in sequential order. If one
object cannot handle a call, it delegates the call to the next in the
chain and so forth.
Examples:
---------
- logging framework, where each chain element decides autonomously what
to do with a log message
- a Spam filter
- Caching: first object is an instance of e.g. a Memcached Interface,
if that "misses" it delegates the call to the database interface
- Yii Framework: CFilterChain is a chain of controller action filters.
the executing point is passed from one filter to the next along the
chain, and only if all filters say "yes", the action can be invoked
at last.
UML Diagram
-----------
.. image:: uml/uml.png
:alt: Alt ChainOfResponsibility UML Diagram
:align: center
Code
----
You can also find these code on `GitHub`_
Handler.php
.. literalinclude:: Handler.php
:language: php
:linenos:
Request.php
.. literalinclude:: Request.php
:language: php
:linenos:
Test
----
Tests/ChainTest.php
.. literalinclude:: Tests/ChainTest.php
:language: php
:linenos:
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/ChainOfResponsibilities

View File

@@ -0,0 +1,76 @@
Command
=======
Purpose
-------
To encapsulate invocation and decoupling.
We have an Invoker and a Receiver. This pattern uses a "Command" to
delegate the method call against the Receiver and presents the same
method "execute". Therefore, the Invoker just knows to call "execute" to
process the Command of the client. The Receiver is decoupled from the
Invoker.
The second aspect of this pattern is the undo(), which undoes the method
execute(). Command can also be aggregated to combine more complex
commands with minimum copy-paste and relying on composition over
inheritance.
Examples
--------
- A text editor : all events are Command which can be undone, stacked
and saved.
- Symfony2: SF2 Commands that can be run from the CLI are built with
just the Command pattern in mind
- big CLI tools use subcommands to distribute various tasks and pack
them in "modules", each of these can be implemented with the Command
pattern (e.g. vagrant)
UML Diagram
-----------
.. image:: uml/uml.png
:alt: Alt Command UML Diagram
:align: center
Code
----
You can also find these code on `GitHub`_
HelloCommand.php
.. literalinclude:: HelloCommand.php
:language: php
:linenos:
Receiver.php
.. literalinclude:: Receiver.php
:language: php
:linenos:
CommandInterface.php
.. literalinclude:: CommandInterface.php
:language: php
:linenos:
Invoker.php
.. literalinclude:: Invoker.php
:language: php
:linenos:
Test
----
Tests/CommandTest.php
.. literalinclude:: Tests/CommandTest.php
:language: php
:linenos:
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/Command

View File

@@ -0,0 +1,69 @@
Iterator
========
Purpose
-------
To make an object iterable and to make it appear like a collection of
objects.
Examples
--------
- to process a file line by line by just running over all lines (which
have an object representation) for a file (which of course is an
object, too)
Note
----
Standard PHP Library (SPL) defines an interface Iterator which is best
suited for this! Often you would want to implement the Countable
interface too, to allow ``count($object)`` on your iterable object
UML Diagram
-----------
.. image:: uml/uml.png
:alt: Alt Iterator UML Diagram
:align: center
Code
----
You can also find these code on `GitHub`_
BookList.php
.. literalinclude:: BookList.php
:language: php
:linenos:
BookListReverseIterator.php
.. literalinclude:: BookListReverseIterator.php
:language: php
:linenos:
BookListIterator.php
.. literalinclude:: BookListIterator.php
:language: php
:linenos:
Book.php
.. literalinclude:: Book.php
:language: php
:linenos:
Test
----
Tests/IteratorTest.php
.. literalinclude:: Tests/IteratorTest.php
:language: php
:linenos:
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/Iterator

View File

@@ -0,0 +1,54 @@
Mediator
========
Purpose
-------
This pattern provides an easy to decouple many components working
together. It is a good alternative over Observer IF you have a "central
intelligence", like a controller (but not in the sense of the MVC).
All components (called Colleague) are only coupled to the
MediatorInterface and it is a good thing because in OOP, one good friend
is better than many. This is the key-feature of this pattern.
UML Diagram
-----------
.. image:: uml/uml.png
:alt: Alt Mediator UML Diagram
:align: center
Code
----
You can also find these code on `GitHub`_
MediatorInterface.php
.. literalinclude:: MediatorInterface.php
:language: php
:linenos:
Mediator.php
.. literalinclude:: Mediator.php
:language: php
:linenos:
Colleague.php
.. literalinclude:: Colleague.php
:language: php
:linenos:
Test
----
Tests/MediatorTest.php
.. literalinclude:: Tests/MediatorTest.php
:language: php
:linenos:
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/Mediator

View File

@@ -0,0 +1,67 @@
Memento
=======
Purpose
-------
Provide the ability to restore an object to its previous state (undo via
rollback).
The memento pattern is implemented with three objects: the originator, a
caretaker and a memento. The originator is some object that has an
internal state. The caretaker is going to do something to the
originator, but wants to be able to undo the change. The caretaker first
asks the originator for a memento object. Then it does whatever
operation (or sequence of operations) it was going to do. To roll back
to the state before the operations, it returns the memento object to the
originator. The memento object itself is an opaque object (one which the
caretaker cannot, or should not, change). When using this pattern, care
should be taken if the originator may change other objects or resources
- the memento pattern operates on a single object.
Examples
--------
- The seed of a pseudorandom number generator
- The state in a finite state machine
UML Diagram
-----------
.. image:: uml/uml.png
:alt: Alt Momento UML Diagram
:align: center
Code
----
You can also find these code on `GitHub`_
Caretaker.php
.. literalinclude:: Caretaker.php
:language: php
:linenos:
Memento.php
.. literalinclude:: Memento.php
:language: php
:linenos:
Originator.php
.. literalinclude:: Originator.php
:language: php
:linenos:
Test
----
Tests/MementoTest.php
.. literalinclude:: Tests/MementoTest.php
:language: php
:linenos:
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/Memento

View File

@@ -0,0 +1,74 @@
Null Object
===========
Purpose
-------
NullObject is not a GoF design pattern but a schema which appears
frequently enough to be considered a pattern. It has the following
benefits:
- Client code is simplified
- Reduces the chance of null pointer exceptions
- Fewer conditionals require less test cases
Methods that return an object or null should instead return an object or
``NullObject``. ``NullObject``\ s simplify boilerplate code such as
``if (!is_null($obj)) { $obj->callSomething(); }`` to just
``$obj->callSomething();`` by eliminating the conditional check in
client code.
Examples
--------
- Symfony2: null logger of profiler
- Symfony2: null output in Symfony/Console
- null handler in a Chain of Responsibilities pattern
- null command in a Command pattern
UML Diagram
-----------
.. image:: uml/uml.png
:alt: Alt NullObject UML Diagram
:align: center
Code
----
You can also find these code on `GitHub`_
PrintLogger.php
.. literalinclude:: PrintLogger.php
:language: php
:linenos:
NullLogger.php
.. literalinclude:: NullLogger.php
:language: php
:linenos:
Service.php
.. literalinclude:: Service.php
:language: php
:linenos:
LoggerInterface.php
.. literalinclude:: LoggerInterface.php
:language: php
:linenos:
Test
----
Tests/LoggerTest.php
.. literalinclude:: Tests/LoggerTest.php
:language: php
:linenos:
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/NullObject

View File

@@ -0,0 +1,57 @@
Observer
========
Purpose
-------
To implement a publish/subscribe behaviour to an object, whenever a
"Subject" object changes it's state, the attached "Observers" will be
notified. It is used to shorten the amount of coupled objects and uses
loose coupling instead.
Examples
--------
- a message queue system is observed to show the progress of a job in a
GUI
Note
----
PHP already defines two interfaces that can help to implement this
pattern: SplObserver and SplSubject.
UML Diagram
-----------
.. image:: uml/uml.png
:alt: Alt Observer UML Diagram
:align: center
Code
----
You can also find these code on `GitHub`_
UserObserver.php
.. literalinclude:: UserObserver.php
:language: php
:linenos:
User.php
.. literalinclude:: User.php
:language: php
:linenos:
Test
----
Tests/ObserverTest.php
.. literalinclude:: Tests/ObserverTest.php
:language: php
:linenos:
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/Observer

View File

@@ -0,0 +1,75 @@
Specification
=============
Purpose
-------
Builds a clear specification of business rules, where objects can be
checked against. The composite specification class has one method called
``isSatisfiedBy`` that returns either true or false depending on whether
the given object satisfies the specification.
UML Diagram
-----------
.. image:: uml/uml.png
:alt: Alt Specification UML Diagram
:align: center
Code
----
You can also find these code on `GitHub`_
Either.php
.. literalinclude:: Either.php
:language: php
:linenos:
PriceSpecification.php
.. literalinclude:: PriceSpecification.php
:language: php
:linenos:
SpecificationInterface.php
.. literalinclude:: SpecificationInterface.php
:language: php
:linenos:
AbstractSpecification.php
.. literalinclude:: AbstractSpecification.php
:language: php
:linenos:
Item.php
.. literalinclude:: Item.php
:language: php
:linenos:
Plus.php
.. literalinclude:: Plus.php
:language: php
:linenos:
Not.php
.. literalinclude:: Not.php
:language: php
:linenos:
Test
----
Tests/SpecificationTest.php
.. literalinclude:: Tests/SpecificationTest.php
:language: php
:linenos:
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/Specification

View File

@@ -0,0 +1,56 @@
State
=====
Purpose
-------
Encapsulate varying behavior for the same routine based on an object's
state. This can be a cleaner way for an object to change its behavior at
runtime without resorting to large monolithic conditional statements.
UML Diagram
-----------
.. image:: uml/uml.png
:alt: Alt State UML Diagram
:align: center
Code
----
You can also find these code on `GitHub`_
OrderInterface.php
.. literalinclude:: OrderInterface.php
:language: php
:linenos:
OrderFactory.php
.. literalinclude:: OrderFactory.php
:language: php
:linenos:
OrderController.php
.. literalinclude:: OrderController.php
:language: php
:linenos:
ShippingOrder.php
.. literalinclude:: ShippingOrder.php
:language: php
:linenos:
CreateOrder.php
.. literalinclude:: CreateOrder.php
:language: php
:linenos:
Test
----
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/State

View File

@@ -0,0 +1,70 @@
Strategy
========
Terminology:
------------
- Context
- Strategy
- Concrete Strategy
Purpose
-------
To separate strategies and to enable fast switching between them. Also
this pattern is a good alternative to inheritance (instead of having an
abstract class that is extended).
Examples
--------
- sorting a list of objects, one strategy by date, the other by id
- simplify unit testing: e.g. switching between file and in-memory
storage
UML Diagram
-----------
.. image:: uml/uml.png
:alt: Alt Strategy UML Diagram
:align: center
Code
----
You can also find these code on `GitHub`_
DateComparator.php
.. literalinclude:: DateComparator.php
:language: php
:linenos:
IdComparator.php
.. literalinclude:: IdComparator.php
:language: php
:linenos:
ObjectCollection.php
.. literalinclude:: ObjectCollection.php
:language: php
:linenos:
ComparatorInterface.php
.. literalinclude:: ComparatorInterface.php
:language: php
:linenos:
Test
----
Tests/StrategyTest.php
.. literalinclude:: Tests/StrategyTest.php
:language: php
:linenos:
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/Strategy

View File

@@ -0,0 +1,63 @@
Template Method
===============
Purpose
-------
Template Method is a behavioral design pattern.
Perhaps you have encountered it many times already. The idea is to let
subclasses of this abstract template "finish" the behavior of an
algorithm.
A.k.a the "Hollywood principle": "Don't call us, we call you." This
class is not called by subclasses but the inverse. How? With abstraction
of course.
In other words, this is a skeleton of algorithm, well-suited for
framework libraries. The user has just to implement one method and the
superclass do the job.
It is an easy way to decouple concrete classes and reduce copy-paste,
that's why you'll find it everywhere.
UML Diagram
-----------
.. image:: uml/uml.png
:alt: Alt TemplateMethod UML Diagram
:align: center
Code
----
You can also find these code on `GitHub`_
CityJourney.php
.. literalinclude:: CityJourney.php
:language: php
:linenos:
Journey.php
.. literalinclude:: Journey.php
:language: php
:linenos:
BeachJourney.php
.. literalinclude:: BeachJourney.php
:language: php
:linenos:
Test
----
Tests/JourneyTest.php
.. literalinclude:: Tests/JourneyTest.php
:language: php
:linenos:
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/TemplateMethod

View File

@@ -0,0 +1,67 @@
Visitor
=======
Purpose
-------
The Visitor Pattern lets you outsource operations on objects to other
objects. The main reason to do this is to keep a separation of concerns.
But classes have to define a contract to allow visitors (the
``Role::accept`` method in the example).
The contract is an abstract class but you can have also a clean
interface. In that case, each Visitor has to choose itself which method
to invoke on the visitor.
UML Diagram
-----------
.. image:: uml/uml.png
:alt: Alt Visitor UML Diagram
:align: center
Code
----
You can also find these code on `GitHub`_
Group.php
.. literalinclude:: Group.php
:language: php
:linenos:
Role.php
.. literalinclude:: Role.php
:language: php
:linenos:
RolePrintVisitor.php
.. literalinclude:: RolePrintVisitor.php
:language: php
:linenos:
User.php
.. literalinclude:: User.php
:language: php
:linenos:
RoleVisitorInterface.php
.. literalinclude:: RoleVisitorInterface.php
:language: php
:linenos:
Test
----
Tests/VisitorTest.php
.. literalinclude:: Tests/VisitorTest.php
:language: php
:linenos:
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/Visitor

40
Behavioral/index.rst Normal file
View File

@@ -0,0 +1,40 @@
Behavioral
==========
In software engineering, behavioral design patterns are design patterns
that identify common communication patterns between objects and realize
these patterns. By doing so, these patterns increase flexibility in
carrying out this communication.
- `ChainOfResponsibilities <ChainOfResponsibilities>`__
`:notebook: <http://en.wikipedia.org/wiki/Chain_of_responsibility_pattern>`__
- `Command <Command>`__
`:notebook: <http://en.wikipedia.org/wiki/Command_pattern>`__
- `Iterator <Iterator>`__
`:notebook: <http://en.wikipedia.org/wiki/Iterator_pattern>`__
- `Mediator <Mediator>`__
`:notebook: <http://en.wikipedia.org/wiki/Mediator_pattern>`__
- `NullObject <NullObject>`__
`:notebook: <http://en.wikipedia.org/wiki/Null_Object_pattern>`__
- `Observer <Observer>`__
`:notebook: <http://en.wikipedia.org/wiki/Observer_pattern>`__
- `Specification <Specification>`__
`:notebook: <http://en.wikipedia.org/wiki/Specification_pattern>`__
- `State <State>`__
`:notebook: <http://en.wikipedia.org/wiki/State_pattern>`__
- `Strategy <Strategy>`__
`:notebook: <http://en.wikipedia.org/wiki/Strategy_pattern>`__
- `TemplateMethod <TemplateMethod>`__
`:notebook: <http://en.wikipedia.org/wiki/Template_method_pattern>`__
- `Visitor <Visitor>`__
`:notebook: <http://en.wikipedia.org/wiki/Visitor_pattern>`__
Code
----
You can also find these code on `GitHub`_
Test
----
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral