Delegated design pattern for common PHP design patterns

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

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.