1. Definition
The Mediator Design Pattern defines an object that encapsulates how a set of objects interact. Rather than objects communicating directly with each other, they go through a mediator. This decouples the classes and reduces the dependencies between them.
2. Problem Statement
Imagine a complex system with numerous objects that interact with each other. Direct communication between these objects leads to tight coupling, making the system hard to maintain and expand. For instance, changing one object might require changes in multiple other objects that are dependent on it.
3. Solution
The Mediator pattern introduces a central object (the mediator) that encapsulates the interaction logic between objects. Instead of objects referring to each other, they refer to the mediator. If any communication needs to occur, the mediator handles and routes it, keeping the objects decoupled.
4. Real-World Use Cases
1. Chat rooms where multiple users send messages, but the display logic is handled centrally.
2. Air traffic control towers managing the take-off and landing of planes.
3. GUI components interact in a complex user interface.
5. Implementation Steps
1. Define the Mediator interface with methods that the components can use.
2. Implement concrete Mediators with interaction logic.
3. Create components (or colleagues) that communicate through the Mediator instead of directly with each other.
6. Implementation in PHP
<?php
// Mediator Interface
interface Mediator {
public function notify($sender, $message);
}
// Concrete Mediator
class ChatRoom implements Mediator {
public function notify($sender, $message) {
echo $sender . " says: " . $message . "\n";
}
}
// Colleague/Component Base Class
abstract class Participant {
protected $name;
protected $mediator;
public function __construct($name, Mediator $mediator) {
$this->name = $name;
$this->mediator = $mediator;
}
public function send($message) {
$this->mediator->notify($this->name, $message);
}
}
// Concrete Colleague/Component
class User extends Participant {}
// Client Code
$chatRoom = new ChatRoom();
$john = new User("John", $chatRoom);
$alice = new User("Alice", $chatRoom);
$john->send("Hi Alice!");
$alice->send("Hey John!");
?>
Output:
John says: Hi Alice! Alice says: Hey John!
Explanation:
1. Mediator: An interface that defines a communication method (notify in this case).
2. ChatRoom: This is a concrete mediator that handles the communication between users.
3. Participant: A base class for components that will communicate with each other via the Mediator.
4. User: A concrete component. It sends messages via the Mediator rather than directly to another User.
5. The client code creates a ChatRoom (mediator) and users (colleagues). When a user sends a message, it's routed through the ChatRoom, which then displays it.
7. When to use?
Use the Mediator pattern when:
1. You want to reduce the complexity and dependencies between objects that interact with each other.
2. You want to centralize external communications.
3. You want to decouple objects so that changes in one won't affect others.
Comments
Post a Comment