Php design mode: delegation mode; php design mode: delegation _ PHP Tutorial

Source: Internet
Author: User
Php design mode: delegation mode; php design mode delegation. Php design mode: delegation mode. php design mode: delegation mode is a basic skill in software design mode. In the delegate mode, two objects are involved in processing the same request, the delegate mode in the php design mode, and the delegate mode in the php design mode.

Delegation modeIt is a basic technique in software design patterns. In the delegate mode, two objects are involved in processing the same request. the object receiving the request delegates the request to another object for processing. Delegation mode is a basic technique. many other modes, such as status mode, policy mode, and visitor mode, use delegation mode in special cases.
Introduction to Dynamic Delegation: The concept of dynamic delegation comes from the Jakarta bytecode Engineering Library (Byte-Code Engineering Library, BCEL ). It can analyze existing classes, and for interfaces, abstract classes, and even specific classes during runtime, it can generate a bytes-encoded delegate class.
The delegated interface/class must meet the following conditions:Dynamic Delegation can only delegate one class, but can proxy multiple interfaces. This restriction comes from the Java single inheritance mode. A Java class can have at most one parent class. Since the generated delegate class regards the delegated class as its parent class, it is unreasonable to specify multiple delegate classes. If the delegate class is not specified, the default parent class is Object.
Below is the PHP Reflection mechanism to implement dynamic proxy code:

<? Phpclass Fruit {function callFruit () {print "Generate an Apple" ;}} class FruitDelegator {private $ targets; function _ construct () {$ this-> target [] = new Fruit ();} function _ call ($ name, $ args) {foreach ($ this-> target as $ obj) {$ r = new ReflectionClass ($ obj); if ($ method = $ r-> getMethod ($ name) {if ($ method-> isPublic ()&&! $ Method-> isAbstract () {return $ method-> invoke ($ obj, $ args) ;}}}$ obj = new FruitDelegator (); $ obj-> callFruit (); // running result // Generate an Apple?>

It can be seen that the proxy class FruitDelegator is used to replace the Fruit class to implement its method.
Similarly, the following code can be run:

<?phpclass Color{  function callColor()  {    print "Generate Red";  }} class ColorDelegator{  private $targets;     function addObject($obj)  {    $this->target[] = $obj;  }     function __call($name, $args)  {    foreach ($this->target as $obj)    {      $r = new ReflectionClass($obj);      if ($method = $r->getMethod($name))      {        if ($method->isPublic() && !$method->isAbstract())        {          return $method->invoke($obj, $args);        }      }    }  }}$obj = new ColorDelegator();$obj->addObject(new Color());$obj->callColor();?>

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

<? Php namespace 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.

I will share with you an example:

<? Php/*** delegate mode example ** @ create_date: 2010-01-04 */class PlayList {var $ _ songs = array (); var $ _ object = null; function PlayList ($ type) {$ object = $ type. "PlayListDelegation"; $ this-> _ object = new $ object ();} function addSong ($ location, $ title) {$ this-> _ songs [] = array ("location" => $ location, "title" => $ title);} function getPlayList () {return $ this-> _ object-> getPlayList ($ this-> _ songs);} class mp3Pla YListDelegation {function getPlayList ($ songs) {$ aResult = array (); foreach ($ songs as $ key => $ item) {$ path = pathinfo ($ item ['location']); if (strtolower ($ item ['extension']) = "mp3 ") {$ aResult [] = $ item;} return $ aResult;} class rmvbPlayListDelegation {function getPlayList ($ songs) {$ aResult = array (); foreach ($ songs as $ key => $ item) {$ path = pathinfo ($ item ['location']); if (strtolower ($ item [' Extension']) = "rmvb") {$ aResult [] = $ item;} return $ aResult ;}}$ oMP3PlayList = new PlayList ("mp3 "); $ oMP3PlayList-> getPlayList (); $ oRMVBPlayList = new PlayList ("rmvb"); $ oRMVBPlayList-> getPlayList ();?>

The above is all the content of this article, hoping to help you learn.

Articles you may be interested in:
  • Php design mode Delegation (Delegation mode)
  • PHP design pattern-decorator pattern
  • Code for implementing the factory design pattern using interfaces in php
  • Two most common design modes of PHP: Factory mode and Singleton mode
  • Summary of php design patterns
  • Php design mode-example
  • Php design mode command mode example
  • PHP design pattern-Observer pattern (Observer) Detailed introduction and code example
  • Simple factory mode for php design patterns
  • Delegated design pattern for common PHP design patterns

Entrust delegation mode is a basic technique in software design mode. In the delegate mode, two objects are involved in processing the same request ,...

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.