The
delegate mode is a basic technique in software design patterns. In delegate mode, two objects participate in the same request, and the object that accepts the request delegates the request to another object to process. Delegate mode is a basic technique, and many other patterns, such as state mode, policy pattern, and visitor pattern, are essentially in a more specific context of delegating mode.
Introduction to Dynamic Delegates : 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 run-time specific classes, it can generate a byte-coded delegate class. The
delegated interface/class should meet the following criteria: A dynamic delegate can delegate up to only one class, but it can broker multiple interfaces. This restriction comes from a single inheritance pattern in Java. A Java class has at most one parent class. Since the generated delegate class takes the delegate class as its parent class, it is unreasonable to specify multiple delegate classes. If no delegate class is specified, then the default parent class is object.
The following is the code for implementing a dynamic proxy by the PHP reflection mechanism:
<?php
class 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
?>
Visible, through proxy class fruitdelegator to replace the fruit class to implement his method.
Similarly, the following code is also capable of running:
<?php
class 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 you improve, using the CD class playback mode, you need to determine in the instantiated class how to choose the playback mode
After the improvement, the playback mode as a parameter passed into the playlist function, automatically can find the corresponding need to play the method.
First, not improved before
<?php
//using the delegate mode before calling the CD class, selecting 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 commissioning mode, the improved CD class
<?php
namespace Tools;
/* Delegate mode
to remove the decision and complex functionality of the core object
//
/Delegate Interface
interface delegate{public
function PlayList ($list, $ song);
}
MP3 Classes class
MP3 implements delegate{public
function PlayList ($list, $song) {return
$list [$song]. mp3 ';
}
}
MP4 Handler 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 ');//As long as you pass parameters to know which playback mode you need to choose
To share one more example:
<?php/** * Delegate Mode sample * * @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 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 entire content of this article, I hope to help you learn.