mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-25 17:21:19 +02:00
PSR-0 compliance
This commit is contained in:
@@ -1,30 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns;
|
||||
namespace DesignPatterns\Observer;
|
||||
|
||||
/**
|
||||
* Observer pattern
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* PHP already defines two interfaces that can help to implement this pattern: SplObserver and SplSubject
|
||||
* Observer pattern : The observed object (the subject)
|
||||
*
|
||||
* The subject maintains a list of Observer and send notificiations.
|
||||
*
|
||||
*/
|
||||
class UserObserver implements \SplObserver
|
||||
{
|
||||
public function update(\SplSubject $subject)
|
||||
{
|
||||
echo get_class($subject) . ' has been updated';
|
||||
}
|
||||
}
|
||||
|
||||
class User implements \SplSubject
|
||||
{
|
||||
|
||||
protected $_data = array();
|
||||
|
||||
/**
|
||||
@@ -86,9 +72,6 @@ class User implements \SplSubject
|
||||
// notify the observers, that user has been updated
|
||||
$this->notify();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$user = new User();
|
||||
$user->attach(new UserObserver());
|
||||
|
||||
$user->notify();
|
26
Observer/UserObserver.php
Normal file
26
Observer/UserObserver.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Observer;
|
||||
|
||||
/**
|
||||
* Observer pattern
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* PHP already defines two interfaces that can help to implement this pattern: SplObserver and SplSubject
|
||||
*
|
||||
*/
|
||||
class UserObserver implements \SplObserver
|
||||
{
|
||||
|
||||
public function update(\SplSubject $subject)
|
||||
{
|
||||
echo get_class($subject) . ' has been updated';
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user