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:
target[] = new Fruit ();} function __call ($name, $args) {foreach ($this->target as $obj) {$r = new Reflectionclass ($obj), if ($method = $r->get Method ($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:
Target[] = $obj;} function __call ($name, $args) {foreach ($this->target as $obj) {$r = new Reflectionclass ($obj), if ($method = $r->get Method ($name)) {if ($method->ispublic () &&! $method->isabstract ()) {return $method->invoke ($obj, $ args);}}}} $obj = new Colordelegator (); $obj->addobject (New Color ()); $obj->callcolor (); >
http://www.bkjia.com/PHPjc/752369.html www.bkjia.com true http://www.bkjia.com/PHPjc/752369.html techarticle 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 ...