Implementation of Delegate mode _php tutorial with PHP reflection

Source: Internet
Author: User
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 ...

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