Observer Design Pattern

The Observer Design Pattern is a behavioral design pattern where an object, called the subject, maintains a list of its dependents, called observers, and notifies them of any state changes, usually by calling one of their methods. Here’s how you can implement the Observer pattern in PHP:

// Subject interface
interface Subject {
    public function attach(Observer $observer);
    public function detach(Observer $observer);
    public function notify();
}

// Concrete subject class
class WeatherStation implements Subject {
    private $temperature;
    private $observers = [];

    public function setTemperature($temperature) {
        $this->temperature = $temperature;
        $this->notify(); // Notify observers when the temperature changes
    }

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

    public function detach(Observer $observer) {
        $index = array_search($observer, $this->observers);
        if ($index !== false) {
            unset($this->observers[$index]);
        }
    }

    public function notify() {
        foreach ($this->observers as $observer) {
            $observer->update($this);
        }
    }

    public function getTemperature() {
        return $this->temperature;
    }
}

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

// Concrete observer classes
class PhoneDisplay implements Observer {
    public function update(Subject $subject) {
        echo "Phone display: Temperature changed to " . $subject->getTemperature() . PHP_EOL;
    }
}

class TVDisplay implements Observer {
    public function update(Subject $subject) {
        echo "TV display: Temperature changed to " . $subject->getTemperature() . PHP_EOL;
    }
}

// Usage
$weatherStation = new WeatherStation();

$phoneDisplay = new PhoneDisplay();
$tvDisplay = new TVDisplay();

$weatherStation->attach($phoneDisplay);
$weatherStation->attach($tvDisplay);

$weatherStation->setTemperature(25); // Output: Phone display: Temperature changed to 25 TV display: Temperature changed to 25

Explanation:

  • We start by defining a Subject interface and a WeatherStation class that implements this interface. The WeatherStation class maintains the temperature and a list of observers (phone displays, TV displays).
  • We define an Observer interface and concrete observer classes (PhoneDisplay and TVDisplay) that implement this interface.
  • The WeatherStation class has methods to attach, detach, and notify observers. When the temperature changes (setTemperature() method), it notifies all registered observers by calling their update() method.
  • Each observer (PhoneDisplay and TVDisplay) updates its display when it receives notification from the WeatherStation.
  • In the usage example, we create a WeatherStation object and attach phone and TV displays as observers. When the temperature changes, both displays are notified and update their display accordingly.

This implementation demonstrates the Observer pattern in PHP, where multiple observers are notified of changes in the subject (weather station) and can react accordingly.

Related Posts

Leave A Comment