PHP design pattern series-intermediary pattern

Source: Internet
Author: User
The intermediary mode is used to develop an object. this object can transfer or mediate changes to the set of similar objects if they do not directly interact with each other. When a non-coupling object with similar properties needs to be synchronized, the best practice is the intermediary mode... SyntaxHighlighter. all (); intermediary mode.
The intermediary mode is used to develop an object, which can transfer or mediate changes to the set of these objects if similar objects do not directly interact with each other. Generally, when processing non-coupling objects with similar properties that need to be synchronized, the best practice is the intermediary mode. PHP is not a commonly used design pattern.
Design scenario:
We have a CD class and an MP3 class. The structure of the two classes is similar.
We need to synchronously update the MP3 class when updating the CD class.
The traditional method is to instantiate the MP3 class in the CD class and then update it. but in this way, the code will be difficult to maintain. if the same MP4 class is added, so we can't deal with it.
The intermediary mode handles this situation well. through the intermediary class, the data can be synchronously updated as long as the intermediary class is called in the CD class.
This design pattern has been used in our phpwind forum before.
Code:
[Php]
Class CD {
Public $ band = '';
Public $ title = '';
Protected $ _ mediator;

Public function _ construct (MusicContainerMediator $ mediator = NULL ){
$ This-> _ mediator = $ mediator;
}

Public function save (){
// Specific implementation to be determined
Var_dump ($ this );
}

Public function changeBandName ($ bandname ){
If (! Is_null ($ this-> _ mediator )){
$ This-> _ mediator-> change ($ this, array ("band" => $ bandname ));
}
$ This-> band = $ bandname;
$ This-> save ();
}
}
// MP3Archive class
Class MP3Archive {
Protected $ _ mediator;

Public function _ construct (MusicContainerMediator $ mediator = NULL ){
$ This-> _ mediator = $ mediator;
}

Public function save (){
// Specific implementation to be determined
Var_dump ($ this );
}

Public function changeBandName ($ bandname ){
If (! Is_null ($ this-> _ mediator )){
$ This-> _ mediator-> change ($ this, array ("band" => $ bandname ));
}
$ This-> band = $ bandname;
$ This-> save ();
}

}
 
// Intermediary class
Class MusicContainerMediator {
Protected $ _ containers = array ();

Public function _ construct (){
$ This-> _ containers [] = "CD ";
$ This-> _ containers [] = "MP3Archive ";
}

Public function change ($ originalObject, $ newValue ){
$ Title = $ originalObject-> title;
$ Band = $ originalObject-> band;
Foreach ($ this-> _ containers as $ container ){
If (! ($ OriginalObject instanceof $ container )){
$ Object = new $ container;
$ Object-> title = $ title;
$ Object-> band = $ band;
Foreach ($ newValue as $ key => $ val ){
$ Object-> $ key = $ val;
}
$ Object-> save ();
}
}
}
}
 
// Test the instance
$ TitleFromDB = "Waste of a Rib ";
$ BandFromDB = "Never Again ";
$ Mediator = new MusicContainerMediator ();
$ Cd = new CD ($ mediator );
$ Cd-> title = $ titleFromDB;
$ Cd-> band = $ bandFromDB;
$ Cd-> changeBandName ("Maybe Once More ");
Author: initphp

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.