Delegation is a method to extend and reuse the functions of a class. Its practice is: write an additional class to provide additional functions, and use the original class instance to provide the original functions. Next, we will introduce the PHP delegated design mode instance through this article, if you are interested, study together.
Mode definition
Delegation is a method to extend and reuse the functions of a class. The method is to write an additional class to provide additional functions, and use the original class instance to provide the original functions.
Assume that we have a TeamLead class that delegates its established tasks to a JuniorDeveloper associated with the auxiliary object: Originally, TeamLead processes the writeCode method, and Usage calls this method of TeamLead, however, TeamLead now delegates the writeCode implementation to the writeBadCode implementation of JuniorDeveloper, but Usage does not perceive that the writeBadCode method is being executed.
A cd class is designed, which includes mp3 and mp4 playback modes.
Before improvement, when using the cd-type playback mode, you need to determine the method of playing in the instantiated class.
After the improvement, the playback mode is passed into the playList function as a parameter, and the corresponding playback method is automatically found.
I. before improvement
<? Php // before using the delegate mode, call the cd class. selecting the cd playing mode is a complicated selection process. class cd {protected $ cdInfo = array (); public function addSong ($ song) {$ this-> cdInfo [$ song] = $ song;} public function playMp3 ($ song) {return $ this-> cdInfo [$ song]. 'authorization';} public function playMp4 ($ song) {return $ this-> cdInfo [$ song]. '.mp4 ';}}$ oldCd = new cd; $ oldCd-> addSong ("1"); $ oldCd-> addSong ("2 "); $ oldCd-> addSong ("3"); $ type = 'mp3'; if ($ type = 'mp3') {$ oldCd-> playMp3 ();} else {$ oldCd-> playMp4 ();}
II. improved cd class through delegation mode
<? Phpnamespace Tools;/* Delegate mode to remove judgments and complex functions from core objects * // Delegate interface Delegate {public function playList ($ list, $ song );} // mp3 processing class mp3 implements Delegate {public function playList ($ list, $ song) {return audio listparts audio song316.'hangzhou ';}} // mp4 processing class mp4 implements Delegate {public function playList ($ list, $ song) {return audio listmedia+song;.'.mp4 ';}} class cdDelegate {protected $ cdInfo = array (); public function addSong ($ song) {$ this-> cdInfo [$ song] = $ song;} public function play ($ type, $ song) {$ name = '\ Tools \\'. $ type; $ obj = new $ name; return $ obj-> playList ($ this-> cdInfo, $ song) ;}$ newCd = new cdDelegate (); $ newCd-> addSong ("1"); $ newCd-> addSong ("2"); $ newCd-> addSong ("3 "); echo $ newCd-> play ('mp3', '1'); // you only need to pass the parameters to know which playback mode to select.
The above content introduces the detailed description of the PHP delegated design mode instance, hoping to help you.