With PHP skills to explain _php reflected achieve entrusting mode - PHP

Source: Hi learning network sensitive and eager Forum www.piaodoo.com welcome to learn from each other

Delegate pattern is a fundamental skill in software design patterns. In entrusting mode, there are two objects involved in the processing the same request, subject receiving a request to delegate the request to another object processing. Delegate pattern is a basic skill, many other models, such as the state model, strategy pattern, the visitor pattern is essentially a more special occasions using the delegate model.

Dynamic Delegation Introduction: Dynamic Delegation concept from Jakarta project library byte code (Byte-Code Engineering Library, BCEL ). It can be analyzed for the presence of the class, and for the interface, the abstract class, even concrete classes at runtime, it is possible to generate coded bytes delegate class.

Entrusted interface / class should satisfy the following conditions: Dynamic Delegation can delegate up a class, but the agent can be a plurality of interfaces. This limitation comes from Java's single inheritance model. A Java class at most one parent. Since the generated class to be commissioned by the delegate class as its parent class, it was commissioned to specify more than one class is unreasonable. If you do not specify the delegate class, the default is the parent class Object.

Here is the code PHP dynamic proxy reflection:

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();
// 运行结果
// Generate an Apple
?>

Visible, instead of through a proxy class FruitDelegator Fruit class to implement his methods.

Similarly, the following code is capable of operating:

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();
?>

The traditional way:

In the traditional way, we need to determine the current operation is a withdrawal or deposit operations, a withdrawal and deposit operations are invoked Bank class.

Delegation Model:

Delegated mode, we would not need the client determination operation, for the client, what operations need be passed directly to the type of operation, it can be automatically determined based Bank operation type, the operation returns the corresponding operation result. When our type of operation a lot of time with the client to determine if else is undoubtedly terrible, and then if we must have in many places this judgment code, we need to determine where the code is modified (adding the judge added later), while the use of commission model, we only need to add the type to places in need in the newly added, it does not require changes elsewhere client code (greatly improves the reusability of code).

to sum up

That's all for this article, I hope the contents of this paper has some reference value of learning for all of us to learn or work, thank you for the support sensitive and eager Forum / Hi learning network. If you want to know more details, please see the related links below

The original address is: http: //www.piaodoo.com/thread-3555-1-1.html

Guess you like

Origin www.cnblogs.com/txdah/p/12093224.html
php