Design patterns: Observer

date: · 3 min
author: Mauto
series: Design Patterns/ part 20/24

The classic Gang of Four patterns, one per part, with practical PHP examples.

  1. Design Patterns
  2. Design patterns: Singleton
  3. Design patterns: Factory Method
  4. Design patterns: Abstract Factory
  5. Design patterns: Builder
  6. Design patterns: Prototype
  7. Design patterns: Adapter
  8. Bridge Pattern in PHP: What It Is, When to Use It, with Examples
  9. Design patterns: Composite
  10. Design patterns: Decorator
  11. Design patterns: Facade, What It Is and How to Use It (PHP)
  12. Design patterns: Flyweight
  13. Design patterns: Proxy
  14. Design patterns: Chain of Responsibility Explained (PHP)
  15. Design patterns: Command
  16. Design patterns: Interpreter
  17. Design patterns: Iterator
  18. Design patterns: Mediator
  19. Design patterns: Memento
  20. Design patterns: Observer
  21. Design patterns: State, What It Is and How to Use It (PHP)
  22. Design patterns: Strategy
  23. Design patterns: Template Method
  24. Design patterns: Visitor
the whole guide >

The Observer Pattern is a behavioral design pattern that defines a one-to-many relationship between objects. When an object changes its state, all the objects observing it (known as observers) are automatically notified and updated. This pattern is useful when you want several objects to react automatically to changes in another object, while keeping loose coupling between the parties involved.

Strengths

  1. Decoupling: It keeps loose coupling between the observed subject and its observers, promoting modularity and flexibility in the code.
  2. Automatic Notifications: Observers are notified automatically when the subject changes state, with no need for explicit interaction between subject and observer.
  3. Easy to Extend: Adding new observers or subjects requires no changes to existing objects, making the system extensible.
  4. Flexibility: It lets you add observers dynamically at runtime, so you can change the system’s behavior without changing its structure.
  5. Use in Reactive Systems: It’s ideal for building reactive systems, where one object’s actions need to propagate efficiently and promptly to other objects.

Use Case

Suppose we have a notification system for a blog. When a new article is published, all subscribed users must be notified by email. The blog is the subject that notifies the observers (the registered users) every time there’s a new article.

PHP Code

// Interface for observers
interface Observer {
    public function update($article);
}

// Interface for the subject (Blog)
interface Subject {
    public function attach(Observer $observer);
    public function detach(Observer $observer);
    public function notify();
}

// Concrete subject class (Blog)
class Blog implements Subject {
    private $observers = [];
    private $latestArticle;

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

    public function detach(Observer $observer) {
        $this->observers = array_filter($this->observers, function($o) use ($observer) {
            return $o !== $observer;
        });
    }

    public function publishArticle($article) {
        $this->latestArticle = $article;
        $this->notify();
    }

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

// Concrete observer class (User)
class User implements Observer {
    private $name;
    
    public function __construct($name) {
        $this->name = $name;
    }

    public function update($article) {
        // The echo simulates sending the email to the user
        echo "Notification for {$this->name}: new article published - {$article}\n";
    }
}

// Usage example

// Create a new blog
$blog = new Blog();

// Create a few users (observers)
$user1 = new User("Alice");
$user2 = new User("Bob");

// The two users subscribe to the blog's notifications
$blog->attach($user1);
$blog->attach($user2);

// A new article is published
$blog->publishArticle("Introduction to Design Patterns");

/* Output:
Notification for Alice: new article published - Introduction to Design Patterns
Notification for Bob: new article published - Introduction to Design Patterns
*/

Explanation

  • Observer: The interface that defines the update() method, which is called every time the subject (the blog) notifies a change.
  • Subject: The interface for the observed subject, with the attach(), detach() and notify() methods for managing observers and notifying changes.
  • Blog: The concrete class that implements the subject. It maintains a list of observers and notifies them of updates when a new article is published.
  • User: The concrete observer class that implements the update() method to receive notifications.

BACK TO THE GUIDE

Copied