PHP — Observer Design Pattern
I. What It Is?
Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
Type: Behavioral
II. Observer Pattern in PHP
A basic implementation in PHP involves creating an Observer
interface with an update
method and a Subject
interface with methods to attach, detach, and notify observers. The concrete implementations, ConcreteSubject
and ConcreteObserver
, would implement these interfaces.
// Observer Interface
interface Observer {
public function update($subject);
}
// Subject Interface
interface Subject {
public function attach(Observer $observer);
public function detach(Observer $observer);
public function notify();
}
// ConcreteSubject Implementation
class ConcreteSubject implements Subject {
private $observers = [];
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);
}
}
}
// ConcreteObserver Implementation
class ConcreteObserver implements Observer {
public function update($subject) {
// Perform update logic
}
}
III. Practical Applications
- Real-time Notifications
Scenario: You have a social media platform where users can follow each other. When a user posts a new message, all their followers should receive a real-time notification. Implementation: Use the Observer Pattern to notify followers when a user posts a new message. The user is the subject, and followers are observers. When the user posts a message, notify all followers to update their feeds. - Stock Market Updates
Scenario: You are building a stock market application that needs to update users in real-time when stock prices change. Implementation: Implement the Observer Pattern to notify users when stock prices change. The stock price is the subject, and users are observers. When the stock price changes, notify all users to update their portfolios. - Task Management System
Scenario: You are developing a task management system where users can assign tasks to team members. When a task is assigned or completed, team members should receive notifications. Implementation: Use the Observer Pattern to notify team members when tasks are assigned or completed. Tasks are subjects, and team members are observers. When a task is assigned or completed, notify the assigned team members to update their task lists.
IV. Interview Questions
- When would you use the Observer Pattern in your application?
“The Observer Pattern is used when there is a need to automatically update dependent objects when the state of another object changes. Examples include implementing an event system, handling changes in data models, and creating a notification system.”
2. What are the components of the Observer Pattern?
- Subject: The object being observed. It maintains a list of observers and notifies them of any changes.
- Observer: The interface for objects that should be notified of changes.
- ConcreteSubject: A concrete implementation of the subject that notifies observers of any changes.
- ConcreteObserver: A concrete implementation of the observer that takes action when notified of a change.
3. How does PHP support the Observer Pattern?
“ PHP provides built-in support for the Observer Pattern through the SplSubject
and SplObserver
interfaces. These provide a standard way to implement the pattern. SplSubject
has attach
, detach
, and notify
methods, while SplObserver
has an update
method.”
4. How does Laravel support the Observer Pattern?
“Laravel has built-in support for Observers through Eloquent model events. You can define observer classes that listen to specific events (like created, updated, or deleted) on models. Observers can be registered in a service provider or directly on the model.”