Lesson 2
Implementing the Observer Pattern in PHP
Introduction to the Observer Pattern in PHP

Welcome back! We're continuing our exploration of Behavioral Patterns. In this lesson, we will delve into the Observer Pattern, a critical design pattern that emphasizes object communication and responsibility distribution. This pattern allows an object, known as the subject, to maintain a list of its dependents, called observers, and automatically notify them of any state changes.

We previously explored the Command Pattern in PHP, which encapsulates a request as an object. Now, let's build on that knowledge and discover how the Observer Pattern facilitates seamless and efficient communication between objects.

What You'll Learn

In this lesson, you will learn how to implement the Observer Pattern by understanding its main components and roles. We'll break down the pattern into manageable parts and demonstrate its practical use with clear PHP examples.

Here's a simple illustration to get you started:

We start by defining a NewsPublisher class that maintains a list of subscribers. When new news is published, the NewsPublisher notifies all subscribers by calling their update method. Each Subscriber can then process the news as needed.

Code Example: Defining the News Publisher Class
php
1class NewsPublisher { 2 private $subscribers = []; 3 4 public function addSubscriber(Subscriber $subscriber) { 5 $this->subscribers[] = $subscriber; 6 } 7 8 public function removeSubscriber(Subscriber $subscriber) { 9 $this->subscribers = array_filter($this->subscribers, function ($sub) use ($subscriber) { 10 return $sub !== $subscriber; 11 }); 12 } 13 14 public function publish($news) { 15 foreach ($this->subscribers as $subscriber) { 16 $subscriber->update($news); 17 } 18 } 19}
Code Example: Defining the Subscriber Interface and Concrete Implementation

Now, let's define the Subscriber interface and a concrete implementation ConcreteSubscriber that prints the received news.

php
1interface Subscriber { 2 public function update($news); 3} 4 5class ConcreteSubscriber implements Subscriber { 6 private $name; 7 8 public function __construct($name) { 9 $this->name = $name; 10 } 11 12 public function update($news) { 13 echo "{$this->name} received news: {$news}\n"; 14 } 15}
Code Example: Demonstrating the Observer Pattern

Here’s how you can use these classes to put the Observer Pattern into practice:

php
1$newsPublisher = new NewsPublisher(); 2$subscriber1 = new ConcreteSubscriber("Subscriber 1"); 3$subscriber2 = new ConcreteSubscriber("Subscriber 2"); 4 5$newsPublisher->addSubscriber($subscriber1); 6$newsPublisher->addSubscriber($subscriber2); 7 8$newsPublisher->publish("Breaking News 1"); 9$newsPublisher->removeSubscriber($subscriber1); 10$newsPublisher->publish("Breaking News 2");

Let's break it down:

  • First, a NewsPublisher object is created, serving as the subject.
  • Next, subscribers are instantiated using the ConcreteSubscriber class.
  • These subscribers are added to the NewsPublisher using the addSubscriber method.
  • When the publish method is called, all registered subscribers are notified and execute their update method, processing the published news.
  • The removeSubscriber method dynamically removes a subscriber, ensuring it no longer receives updates when publish is called again.
  • This sequence demonstrates how the Observer Pattern ensures real-time updates to all observers while allowing dynamic changes to the subscriber list.
Key Components of the Observer Pattern

Let's explore the components of the Observer Pattern with PHP examples:

  • Subject (NewsPublisher): This class maintains a list of observers and notifies them of any state changes. The NewsPublisher class handles this behavior in the PHP code.
  • Observer (Subscriber): This interface defines a method for receiving updates from the subject. The update method must be implemented by any concrete subscribers.
  • Concrete Observer (ConcreteSubscriber): Implements the Subscriber interface to receive updates. It processes the update and prints the news.
  • Client Code: The PHP script demonstrates creating a NewsPublisher, adding subscribers, publishing news, and removing subscribers.
Why It Matters

Mastering the Observer Pattern is crucial for designing systems where objects need to maintain synchronized states in PHP applications. This pattern facilitates loose coupling, enhances code readability, and improves maintainability.

Consider a real-world scenario of a news publishing system in a web application where multiple users receive updates whenever new articles are published. The Observer Pattern ensures that all subscribers are automatically notified of new news, without the publisher maintaining direct dependencies on each subscriber. This design greatly simplifies future extensions, allowing new subscriber types to be added without modifying existing code.

Excited to leverage the power of the Observer Pattern? Let's move on to practice these concepts in action!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.