PHP design mode delegate mode, PHP design mode delegate _php Tutorial

Source: Internet
Author: User

PHP design mode delegate mode, PHP design mode delegate


The delegation mode is a basic technique in software design pattern. In delegate mode, there are two objects involved in processing the same request, and the object that accepts the request delegates the request to another object for processing. The delegate mode is a basic technique, and many other patterns, such as state mode, policy mode, and visitor pattern, are in essence a delegate mode in more special situations.
Introduction to Dynamic Delegation: The concept of dynamic delegation comes from Jakarta bytecode Engineering Library (Byte-code Engineering Library, BCEL). It is able to parse the existence of classes, and for interfaces, abstract classes, and even runtime-specific classes, it is able to generate byte-coded delegate classes.
the delegated interface/class should meet the following criteria: a dynamic delegate can delegate at most one class, but can proxy multiple interfaces. This restriction comes from Java's single-inheritance model. A Java class has at most one parent class. Since the generated delegate class takes 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.
Here is the code for the dynamic proxy implemented by the PHP reflection mechanism:

<?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 ();//Run result//Generate an apple?>

It can be seen that the proxy class fruitdelegator instead of the fruit class to implement his method.
Similarly, the following code is also capable of running:

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

First, not improved before

<?php//Use the 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;  }    Public Function PlayMp3 ($song) {   return $this->cdinfo[$song]. '. mp3 ';  }    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

<?php namespace Tools; /* Delegate mode removes judgments and complex functionality in core objects *//Delegate Interface interface delegate{Public function playList ($list, $song);}//mp3 processing class mp3 Implements delegate{Public Function playList ($list, $song) {  return $list [$song]. MP3 '; }}//mp4 handles class MP4 implements delegate{Public Function playList ($list, $song) {  return $list [$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 ');//Just pass the parameters to know which playback mode to choose

Let's share an example for you:

<?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" and "$title");} function Getplaylist () {return $this->_object->getplaylist ($this->_songs);}}  Class mp3playlistdelegation{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; }} $oMP 3PlayList = new PlayList ("MP3"); $oMP 3playlist->getpLaylist (); $oRMVBPlayList = new PlayList ("rmvb"); $oRMVBPlayList->getplaylist (); >

The above is the whole content of this article, I hope that everyone's study has helped.

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
    • design mode of design for PHP in common use

http://www.bkjia.com/PHPjc/1099069.html www.bkjia.com true http://www.bkjia.com/PHPjc/1099069.html techarticle PHP design mode of the delegation mode, PHP design mode commissioned by the model is a software design model of a basic skill. In delegate mode, there are two objects involved in processing the same request, ...

  • Related Article

    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.