PHP design mode-intermediary mode

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.