PROGRAMMING

  MINTE9
REMEMBERS




Last update:   29-10-2021

Observer Pattern

It is used when we have several objects that are dependent of another object. When a Subject object changes its state, the attached Observers will be notified.
 
// index.php

namespace DesignPatterns\Observer;

require_once __DIR__ . "/../../autoloader.php";

$app = new App();

$guestModel = new AppGuestModel();
$userModel = new AppUserModel();

$app->attachObserver($guestModel);
$app->attachObserver($userModel);

$app->userId = 123;
$app->notifyObservers();

echo $guestModel->userId; // 123
echo $userModel->userId; // 123
 
// autoload.php

function classAutoloader($class) {

    $tmp = explode("\\", $class);
    require_once join("/", $tmp) . ".php";
}

spl_autoload_register("classAutoloader");

Interfaces

 
// Subject.php

namespace DesignPatterns\Observer;

interface Subject {
    public function attachObserver(Observer $observer);
    public function detachObserver(Observer $observer);
    public function notifyObservers();
}
 
// Observer.php

namespace DesignPatterns\Observer;

interface Observer {
    public function update(Subject $subject);
}

Subject

 
// App.php

namespace DesignPatterns\Observer;

class App implements Subject {

    private $observers = [];
    public $userId;

    public function attachObserver(Observer $o) {
        $this->observers[] = $o;
    }

    public function detachObserver(Observer $o) {
        $this->observers = array_diff(
            $this->observers, array($o)
        );
    }

    public function notifyObservers() {
        foreach($this->observers as $o) {
            $o->update($this);
        }
    }

}

Observers

 
// AppUserModel.php

namespace DesignPatterns\Observer;

class AppUserModel extends AppBaseModel {

    public $userId;
}
 
// AppGuestModel.php

namespace DesignPatterns\Observer;

class AppGuestModel extends AppBaseModel {

    public $userId;
}
 
// AppBaseModel.php

namespace DesignPatterns\Observer;

class AppBaseModel implements Observer {

    // Override
    public function update(Subject $subject) {

        foreach(get_object_vars($subject) as $k=>$v) {

            if (property_exists($this, $k)) {

                $this->$k = $v;
            }
        }
    }
}
Spl Library    (2/3)

SPL Library

PHP already has standard libraries (SplObserver, SplSubject).
 
// index.php

declare(strict_types=1);

namespace DesignPatterns\Observer;

require_once __DIR__ . "/../../autoloader.php";

$app = new App();

$guestModel = new AppGuestModel();
$userModel = new AppUserModel();

$app->attach($guestModel);
$app->attach($userModel);

$app->setUserId(123);
//$app->setUserId("123"); // throws error: must be nteger

echo $userModel->userId; // 123
echo $guestModel->userId; // 123
 
// App.php

declare(strict_types=1);

namespace DesignPatterns\Observer;

use SplSubject;
use SplObserver;
use SplObjectStorage;

class App implements SplSubject
{
    private $observers;
    public $userId;

    public function __construct()
    {
        $this->observers = new SplObjectStorage();
    }

    public function setUserId(int $userId): void
    {
        $this->userId = $userId;

        $this->notify(); // notify observers
    }

    // @Override
    public function attach(SplObserver $o)
    {
        $this->observers->attach($o);
    }

    // @Override
    public function detach(SplObserver $o)
    {
        $this->observers->dettach($o);
    }

    // @Override
    public function notify()
    {
        foreach($this->observers as $o) {
            $o->update($this);
        }
    }
}
 
// AppGuestModel.php

namespace DesignPatterns\Observer;

use SplSubject;
use SplObserver;

class AppGuestModel implements SplObserver
{
    public $userId;

    // @Override
    public function update(SplSubject $subject)
    {
        $this->userId = $subject->userId;
    }
}
 
// AppUserModel.php

namespace DesignPatterns\Observer;

use SplSubject;
use SplObserver;

class AppUserModel implements SplObserver
{
    public $userId;

    // @Override
    public function update(SplSubject $subject)
    {
        $this->userId = $subject->userId;
    }
}
Compact    (3/3)

Compact

Observer pattern in one file.
 
// Subject
class App implements SplSubject
{
    private $observers;
    public $userId;

    public function __construct()
    {
        $this->observers = new SplObjectStorage();
    }

    public function setUserId(int $userId): void
    {
        $this->userId = $userId;

        $this->notify(); // notify observers
    }

    // @Override
    public function attach(SplObserver $o)
    {
        $this->observers->attach($o);
    }

    // @Override
    public function detach(SplObserver $o)
    {
        $this->observers->dettach($o);
    }

    // @Override
    public function notify()
    {
        foreach($this->observers as $o) {
            $o->update($this);
        }
    }
}

// Observer
class AppGuestModel implements SplObserver
{
    public $userId;

    // @Override
    public function update(SplSubject $subject)
    {
        $this->userId = $subject->userId;
    }
}

// Observer
class AppUserModel implements SplObserver
{
    public $userId;

    // @Override
    public function update(SplSubject $subject)
    {
        $this->userId = $subject->userId;
    }
}

$app = new App();

$guestModel = new AppGuestModel();
$userModel = new AppUserModel();

$app->attach($guestModel);
$app->attach($userModel);

$app->setUserId(123);

echo $userModel->userId; // 123
echo $guestModel->userId; // 123

Questions    
Strategy Pattern
        A B C D E F
🔔
1/2