PHP design pattern-intermediary pattern Statement: This series of blog reference material "big talk design pattern", author Cheng Jie.
The intermediary mode uses an intermediary object to encapsulate a series of object interactions. The intermediary makes it unnecessary for objects to explicitly reference each other so that they are loosely coupled and the interaction between them can be changed independently.
UML class diagram:
Role:
UnitedNations: defines the methods required for interaction between colleagues.
UnitedCommit: it needs to understand and maintain each colleague object, and coordinates the interaction relationship of each colleague object.
Colleague class definition (Country): usually implemented as an abstract class, mainly responsible for restricting the type of colleague objects and implementing public functions between specific colleague classes,
Specific colleague class (China): to implement your own business, you need to notify the intermediary object when interacting with other colleagues' objects. the intermediary object will be responsible for subsequent interaction.
Core code:
Mediator =$ _ mediator ;}/// Country class // U.S. class USA extends Country {function _ construct (UnitedNations $ mediator) {parent :: __construct ($ mediator);} // declare public function Declared ($ message) {$ this-> mediator-> Declared ($ message, $ this );} // get the message public function GetMessage ($ message) {echo "us get the recipient message: $ message
";}}// China class China extends Country {public function _ construct (UnitedNations $ mediator) {parent ::__ construct ($ mediator );} // declare the public function Declared ($ message) {$ this-> mediator-> Declared ($ message, $ this) ;}// obtain the message public function GetMessage ($ message) {echo "Chinese side receives the other party's message: $ message
";}}// Abstract intermediary // abstract United Nations agency abstract class UnitedNations {// declare public abstract function Declared ($ message, Country $ colleague );} // United Nations institution class UnitedCommit extends UnitedNations {public $ countryUsa; public $ countryChina; // declare public function Declared ($ message, Country $ colleague) {if ($ colleague ==$ this-> countryChina) {$ this-> countryUsa-> GetMessage ($ message );} else {$ this-> countryChina-> GetMessage ($ message );}}}
Call the client test code:
Header ("Content-Type: text/html; charset = utf-8"); // -------------------------- intermediary mode ------------------- require_once ". /Mediator. php "; // test code block $ UNSC = new UnitedCommit (); $ c1 = new USA ($ UNSC); $ c2 = new China ($ UNSC ); $ UNSC-> countryChina = $ c2; $ UNSC-> countryUsa = $ c1; $ c1-> Declared ("Yao Ming plays basketball well "); $ c2-> Declared ("thank you. ");
Applicable scenarios:
1. if the communication mode between a group of objects is complex, resulting in mutual dependency and chaotic structure, you can use the intermediary mode.
2. if an object references many objects and interacts with these objects, it is difficult to reuse the object.
Welcome to my video course. The address is as follows. thank you.
PHP object-oriented design model