In the previous article "In-depth analysis of the combination mode in PHP" we introduced the combination mode in PHP. The following article will take you to understand the intermediary mode in the PHP design pattern.
As mentioned last time, those of us who work outside the home often have deep contact with one type of person, that is, real estate agents. The second-generation X generation who can buy a house in their favorite city right after graduating from college are not within the scope of our consideration. Since you need to rent a house for a long time, you will inevitably have to deal with a real estate agent every one or two or three to five years due to changes in work or life. Sometimes, when we rent a house, we don’t necessarily know the landlord’s information, and the landlord doesn’t need to know our information. Everything is handled by the intermediary. Here, the intermediary becomes our bridge of communication. This situation is actually like the homeowner going abroad or having something to do overseas and leaving the house completely in the hands of the intermediary. Similar to this situation, in the code world, it is a typical application of the mediator pattern.
GoF definition: Use an intermediary object to encapsulate a series of object interactions. Mediators eliminate the need for objects to explicitly reference each other, making them loosely coupled and allowing them to independently change the interactions between them
GoF Class Diagram
Code implementation
abstract class Mediator { abstract public function Send(String $message, Colleague $colleague); } class ConcreteMediator extends Mediator { public $colleague1; public $colleague2; public function Send(String $message, Colleague $colleague) { if ($colleague == $this->colleague1) { $this->colleague2->Notify($message); } else { $this->colleague1->Notify($message); } } }
Abstract mediator and specific implementation, here, we assume that there are two fixed The colleague class allows them to talk to each other, so when the entering colleague is 1, call the Notify method of 2, which is equivalent to letting 2 receive the message from 1
abstract class Colleague { protected $mediator; public function __construct(Mediator $mediator) { $this->mediator = $mediator; } } class ConcreteColleague1 extends Colleague { public function Send(String $message) { $this->mediator->Send($message, $this); } public function Notify(String $message) { echo "同事1得到信息:" . $message, PHP_EOL; } } class ConcreteColleague2 extends Colleague { public function Send(String $message) { $this->mediator->Send($message, $this); } public function Notify(String $message) { echo "同事2得到信息:" . $message; } }
Colleague class and specific implementation, One thing we need to confirm here is that each colleague type only knows the intermediary and does not know other colleague types. This is the characteristic of the intermediary, and the two parties do not need to know each other.
$m = new ConcreteMediator(); $c1 = new ConcreteColleague1($m); $c2 = new ConcreteColleague2($m); $m->colleague1 = $c1; $m->colleague2 = $c2; $c1->Send("吃过饭了吗?"); $c2->Send("没有呢,你打算请客?");
The client call is relatively simple!
As An entrepreneur knows the importance of project management, and the project manager plays the role of an intermediary on many occasions. From an organizational perspective, at the beginning and end of a project, as the boss, I don't need to care who does the coding. The person I want to communicate with is just the project manager. In the same way, other auxiliary departments include finance, human resources, administration, etc. They do not care who writes the code, but only need to communicate with the project manager to understand the status of the project and what needs to be coordinated. In the project team, what about the people who write code? There is no need to know who will pay him wages or where the attendance problem is. All these can be solved by the project manager. Therefore, project development under the project manager responsibility system is a typical application of the intermediary model. The reason why our mobile phone factory is developing so fast is thanks to these project managers. Let’s treat them to a big dinner in the evening~~~
Full code: https:// github.com/zhangyue0503/designpatterns-php/blob/master/15.mediator/source/mediator.php
We will not post it this time Now that you have text messages, let’s implement a chat room. The requirement for a simple online chat room is to allow users who enter the chat room to chat online. Let’s take a look at how to implement this chat room using the intermediary mode!
Chat room class diagram
Full source code: https://github.com/zhangyue0503/designpatterns-php /blob/master/15.mediator/source/mediator-webchat.php
<?php abstract class Mediator { abstract public function Send($message, $user); } class ChatMediator extends Mediator { public $users = []; public function Attach($user) { if (!in_array($user, $this->users)) { $this->users[] = $user; } } public function Detach($user) { $position = 0; foreach ($this->users as $u) { if ($u == $user) { unset($this->users[$position]); } $position++; } } public function Send($message, $user) { foreach ($this->users as $u) { if ($u == $user) { continue; } $u->Notify($message); } } } abstract class User { public $mediator; public $name; public function __construct($mediator, $name) { $this->mediator = $mediator; $this->name = $name; } } class ChatUser extends User { public function Send($message) { $this->mediator->Send($message . '(' . $this->name . '发送)', $this); } public function Notify($message) { echo $this->name . '收到消息:' . $message, PHP_EOL; } } $m = new ChatMediator(); $u1 = new ChatUser($m, '用户1'); $u2 = new ChatUser($m, '用户2'); $u3 = new ChatUser($m, '用户3'); $m->Attach($u1); $m->Attach($u3); $m->Attach($u2); $u1->Send('Hello, 大家好呀!'); // 用户2、用户3收到消息 $u2->Send('你好呀!'); // 用户1、用户3收到消息 $m->Detach($u2); // 用户2退出聊天室 $u3->Send('欢迎欢迎!'); // 用户1收到消息
Description
Original address: https://juejin.cn/post/6844903975192363015
Author: Hardcore Project Manager
Recommended Study: "PHP Video Tutorial"
The above is the detailed content of A brief discussion on the intermediary model in PHP. For more information, please follow other related articles on the PHP Chinese website!