mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-07 07:26:32 +02:00
execute read-the-docs.sh
This commit is contained in:
57
Behavioral/Observer/index.rst
Normal file
57
Behavioral/Observer/index.rst
Normal 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
|
Reference in New Issue
Block a user