Skip to content

Commit bba8b0d

Browse files
committed
execute read-the-docs.sh
1 parent c6cc7f2 commit bba8b0d

File tree

40 files changed

+2491
-0
lines changed

40 files changed

+2491
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
Chain Of Responsibilities
2+
=========================
3+
4+
Purpose:
5+
--------
6+
7+
To build a chain of objects to handle a call in sequential order. If one
8+
object cannot handle a call, it delegates the call to the next in the
9+
chain and so forth.
10+
11+
Examples:
12+
---------
13+
14+
- logging framework, where each chain element decides autonomously what
15+
to do with a log message
16+
- a Spam filter
17+
- Caching: first object is an instance of e.g. a Memcached Interface,
18+
if that "misses" it delegates the call to the database interface
19+
- Yii Framework: CFilterChain is a chain of controller action filters.
20+
the executing point is passed from one filter to the next along the
21+
chain, and only if all filters say "yes", the action can be invoked
22+
at last.
23+
24+
UML Diagram
25+
-----------
26+
27+
.. image:: uml/uml.png
28+
:alt: Alt ChainOfResponsibility UML Diagram
29+
:align: center
30+
31+
Code
32+
----
33+
34+
You can also find these code on `GitHub`_
35+
36+
Handler.php
37+
38+
.. literalinclude:: Handler.php
39+
:language: php
40+
:linenos:
41+
42+
Request.php
43+
44+
.. literalinclude:: Request.php
45+
:language: php
46+
:linenos:
47+
48+
Test
49+
----
50+
51+
Tests/ChainTest.php
52+
53+
.. literalinclude:: Tests/ChainTest.php
54+
:language: php
55+
:linenos:
56+
57+
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/ChainOfResponsibilities

Behavioral/Command/index.rst

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
Command
2+
=======
3+
4+
Purpose
5+
-------
6+
7+
To encapsulate invocation and decoupling.
8+
9+
We have an Invoker and a Receiver. This pattern uses a "Command" to
10+
delegate the method call against the Receiver and presents the same
11+
method "execute". Therefore, the Invoker just knows to call "execute" to
12+
process the Command of the client. The Receiver is decoupled from the
13+
Invoker.
14+
15+
The second aspect of this pattern is the undo(), which undoes the method
16+
execute(). Command can also be aggregated to combine more complex
17+
commands with minimum copy-paste and relying on composition over
18+
inheritance.
19+
20+
Examples
21+
--------
22+
23+
- A text editor : all events are Command which can be undone, stacked
24+
and saved.
25+
- Symfony2: SF2 Commands that can be run from the CLI are built with
26+
just the Command pattern in mind
27+
- big CLI tools use subcommands to distribute various tasks and pack
28+
them in "modules", each of these can be implemented with the Command
29+
pattern (e.g. vagrant)
30+
31+
UML Diagram
32+
-----------
33+
34+
.. image:: uml/uml.png
35+
:alt: Alt Command UML Diagram
36+
:align: center
37+
38+
Code
39+
----
40+
41+
You can also find these code on `GitHub`_
42+
43+
HelloCommand.php
44+
45+
.. literalinclude:: HelloCommand.php
46+
:language: php
47+
:linenos:
48+
49+
Receiver.php
50+
51+
.. literalinclude:: Receiver.php
52+
:language: php
53+
:linenos:
54+
55+
CommandInterface.php
56+
57+
.. literalinclude:: CommandInterface.php
58+
:language: php
59+
:linenos:
60+
61+
Invoker.php
62+
63+
.. literalinclude:: Invoker.php
64+
:language: php
65+
:linenos:
66+
67+
Test
68+
----
69+
70+
Tests/CommandTest.php
71+
72+
.. literalinclude:: Tests/CommandTest.php
73+
:language: php
74+
:linenos:
75+
76+
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/Command

Behavioral/Iterator/index.rst

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
Iterator
2+
========
3+
4+
Purpose
5+
-------
6+
7+
To make an object iterable and to make it appear like a collection of
8+
objects.
9+
10+
Examples
11+
--------
12+
13+
- to process a file line by line by just running over all lines (which
14+
have an object representation) for a file (which of course is an
15+
object, too)
16+
17+
Note
18+
----
19+
20+
Standard PHP Library (SPL) defines an interface Iterator which is best
21+
suited for this! Often you would want to implement the Countable
22+
interface too, to allow ``count($object)`` on your iterable object
23+
24+
UML Diagram
25+
-----------
26+
27+
.. image:: uml/uml.png
28+
:alt: Alt Iterator UML Diagram
29+
:align: center
30+
31+
Code
32+
----
33+
34+
You can also find these code on `GitHub`_
35+
36+
BookList.php
37+
38+
.. literalinclude:: BookList.php
39+
:language: php
40+
:linenos:
41+
42+
BookListReverseIterator.php
43+
44+
.. literalinclude:: BookListReverseIterator.php
45+
:language: php
46+
:linenos:
47+
48+
BookListIterator.php
49+
50+
.. literalinclude:: BookListIterator.php
51+
:language: php
52+
:linenos:
53+
54+
Book.php
55+
56+
.. literalinclude:: Book.php
57+
:language: php
58+
:linenos:
59+
60+
Test
61+
----
62+
63+
Tests/IteratorTest.php
64+
65+
.. literalinclude:: Tests/IteratorTest.php
66+
:language: php
67+
:linenos:
68+
69+
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/Iterator

Behavioral/Mediator/index.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
Mediator
2+
========
3+
4+
Purpose
5+
-------
6+
7+
This pattern provides an easy to decouple many components working
8+
together. It is a good alternative over Observer IF you have a "central
9+
intelligence", like a controller (but not in the sense of the MVC).
10+
11+
All components (called Colleague) are only coupled to the
12+
MediatorInterface and it is a good thing because in OOP, one good friend
13+
is better than many. This is the key-feature of this pattern.
14+
15+
UML Diagram
16+
-----------
17+
18+
.. image:: uml/uml.png
19+
:alt: Alt Mediator UML Diagram
20+
:align: center
21+
22+
Code
23+
----
24+
25+
You can also find these code on `GitHub`_
26+
27+
MediatorInterface.php
28+
29+
.. literalinclude:: MediatorInterface.php
30+
:language: php
31+
:linenos:
32+
33+
Mediator.php
34+
35+
.. literalinclude:: Mediator.php
36+
:language: php
37+
:linenos:
38+
39+
Colleague.php
40+
41+
.. literalinclude:: Colleague.php
42+
:language: php
43+
:linenos:
44+
45+
Test
46+
----
47+
48+
Tests/MediatorTest.php
49+
50+
.. literalinclude:: Tests/MediatorTest.php
51+
:language: php
52+
:linenos:
53+
54+
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/Mediator

Behavioral/Memento/index.rst

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
Memento
2+
=======
3+
4+
Purpose
5+
-------
6+
7+
Provide the ability to restore an object to its previous state (undo via
8+
rollback).
9+
10+
The memento pattern is implemented with three objects: the originator, a
11+
caretaker and a memento. The originator is some object that has an
12+
internal state. The caretaker is going to do something to the
13+
originator, but wants to be able to undo the change. The caretaker first
14+
asks the originator for a memento object. Then it does whatever
15+
operation (or sequence of operations) it was going to do. To roll back
16+
to the state before the operations, it returns the memento object to the
17+
originator. The memento object itself is an opaque object (one which the
18+
caretaker cannot, or should not, change). When using this pattern, care
19+
should be taken if the originator may change other objects or resources
20+
- the memento pattern operates on a single object.
21+
22+
Examples
23+
--------
24+
25+
- The seed of a pseudorandom number generator
26+
- The state in a finite state machine
27+
28+
UML Diagram
29+
-----------
30+
31+
.. image:: uml/uml.png
32+
:alt: Alt Momento UML Diagram
33+
:align: center
34+
35+
Code
36+
----
37+
38+
You can also find these code on `GitHub`_
39+
40+
Caretaker.php
41+
42+
.. literalinclude:: Caretaker.php
43+
:language: php
44+
:linenos:
45+
46+
Memento.php
47+
48+
.. literalinclude:: Memento.php
49+
:language: php
50+
:linenos:
51+
52+
Originator.php
53+
54+
.. literalinclude:: Originator.php
55+
:language: php
56+
:linenos:
57+
58+
Test
59+
----
60+
61+
Tests/MementoTest.php
62+
63+
.. literalinclude:: Tests/MementoTest.php
64+
:language: php
65+
:linenos:
66+
67+
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/Memento

0 commit comments

Comments
 (0)