How to implement the Mediator design pattern in PHP for communication between objects?

by arnoldo.moen , in category: PHP General , 2 years ago

How to implement the Mediator design pattern in PHP for communication between objects?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by mac , 2 years ago

@arnoldo.moen 

The Mediator design pattern is a behavioral pattern that facilitates communication between objects by encapsulating the way they interact with each other. In this pattern, a mediator object acts as a central hub that controls the communication between the participating objects. Here's how to implement the Mediator design pattern in PHP:

  1. Define the Mediator interface: Define an interface that declares the methods that the Mediator object should implement to facilitate communication between the objects.
1
2
3
interface Mediator {
    public function sendMessage(string $message, Colleague $colleague);
}


  1. Define the Colleague interface: Define an interface that declares the methods that the participating objects should implement to communicate with the Mediator object.
1
2
3
4
5
interface Colleague {
    public function setMediator(Mediator $mediator);
    public function sendMessage(string $message);
    public function receiveMessage(string $message);
}


  1. Implement the Mediator class: Create a concrete class that implements the Mediator interface. This class should maintain a list of participating objects and provide methods to facilitate communication between them.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class ConcreteMediator implements Mediator {
    private $colleagues = [];

    public function addColleague(Colleague $colleague) {
        $this->colleagues[] = $colleague;
    }

    public function sendMessage(string $message, Colleague $colleague) {
        foreach($this->colleagues as $col) {
            if($col != $colleague) {
                $col->receiveMessage($message);
            }
        }
    }
}


  1. Implement the Colleague class: Create a concrete class that implements the Colleague interface. This class should maintain a reference to the Mediator object and use it to communicate with other participating objects.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class ConcreteColleague implements Colleague {
    private $mediator;

    public function setMediator(Mediator $mediator) {
        $this->mediator = $mediator;
    }

    public function sendMessage(string $message) {
        $this->mediator->sendMessage($message, $this);
    }

    public function receiveMessage(string $message) {
        echo "Received message: " . $message . "
";
    }
}


  1. Use the Mediator and Colleague objects: Create instances of the Mediator and Colleague objects and add the Colleague objects to the Mediator's list of participating objects. Then, use the Colleague objects to send messages to each other through the Mediator object.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
$mediator = new ConcreteMediator();

$colleague1 = new ConcreteColleague();
$colleague2 = new ConcreteColleague();
$colleague3 = new ConcreteColleague();

$colleague1->setMediator($mediator);
$colleague2->setMediator($mediator);
$colleague3->setMediator($mediator);

$mediator->addColleague($colleague1);
$mediator->addColleague($colleague2);
$mediator->addColleague($colleague3);

$colleague1->sendMessage("Hello, colleagues!");
$colleague2->sendMessage("Hi, colleague 1!");
$colleague3->sendMessage("Hey, everyone!");

// Output:
// Received message: Hello, colleagues!
// Received message: Hi, colleague 1!
// Received message: Hey, everyone!


In the example above, the ConcreteMediator object acts as a central hub for communication between the ConcreteColleague objects. When a ConcreteColleague object sends a message through its Mediator object, the Mediator object relays the message to all the other participating ConcreteColleague objects except for the sender.