PHP design mode of the use of commissioned design mode, PHP commissioned design mode
Pattern definition
A delegate is a way to extend and reuse the functionality of a class. It does this by writing an additional class to provide additional functionality and using an instance of the original class to provide the original functionality.
Let's say we have a teamlead class that delegates its assigned task to an associated helper juniordeveloper to do it: the Teamlead handles the Writecode method, the usage calls the Teamlead method, but now Teamlead will The implementation of Writecode is delegated to Juniordeveloper's Writebadcode, but Usage is not perceived in the execution Writebadcode method.
A CD class was designed with MP3 playback mode and MP4 playback mode in the class.
Before the improvement, using the CD class playback mode, you need in the instantiated class to determine what mode of playback mode to choose
After the improvement, the playback mode as a parameter passed into the playlist function, it will automatically find the corresponding method to play.
One, not improved before
<?php//Use delegate mode before calling the CD class, select CD playback mode is a complex selection process class CD {protected $cdInfo = array (); Public function Addsong ($song) {$this- >cdinfo[$song] = $song; The Public Function playMp3 ($song) {return $this->cdinfo[$song]. '. mp3 '; The 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 ();}
Second, through the commissioned model, improved CD class
<?phpnamespace tools;/* Delegate mode removes judgments and complex functional *///delegate interfaces in core objects interface Delegate{public function PlayList ($list, $song);} MP3 Processing class MP3 implements Delegate{public function PlayList ($list, $song) {return $list [$song]. mp3 ';}} MP4 Processing class MP4 implements Delegate{public function PlayList ($list, $song) {return $list [$song]. mp4 ';}} Class cddelegate{protected $cdInfo = Array ();p ublic 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 ');//Just pass the parameters to know which playback mode to choose
The above content to introduce the PHP commissioned design model Examples of detailed, I hope to be helpful to everyone.
Articles you may be interested in:
- PHP design mode Delegation (delegate mode)
- The decorator mode of PHP design mode
- Code for using interfaces in PHP to implement factory design patterns
- PHP most commonly used 2 design mode Factory mode and a singleton mode introduction
- Summary of PHP Design Patterns
- Example of PHP design pattern using a singleton pattern
- Example of command pattern usage in PHP design mode
- PHP Design Pattern Viewer pattern (Observer) details and code examples
- Simple factory model of PHP design mode
- The delegate mode of PHP design mode
http://www.bkjia.com/PHPjc/1099070.html www.bkjia.com true http://www.bkjia.com/PHPjc/1099070.html techarticle PHP is a common design mode of the commissioned design mode, PHP commissioned design mode definition of a delegate is the function of a class to extend and reuse the method. It's done: Write an additional class ...