Proxy (agent mode) and facade (appearance) design mode
They all provide abstractions for more complex functions, but these two processes are very different in their abstraction.
Proxy case, all methods and member variables are from the target object, and if necessary, the agent can modify or check the data it passes
Magic Method makes the implementation of the proxy simple, a class of proxy mode used to record the access information of the method
You can also use the class of proxy to determine the scope of the code or the problems that exist in the debug program
<?PHPclassloggingproxy{Private $target; //pass in an object Public function__construct ($target){ $this->target=$target; } protected function Log($line){ Echo"[".$line."] \ r \ n "; } Public function__set ($name,$value){ $this->target->$name=$value; $this-Log("Setting Value for$name:$value"); } Public function__get ($name){ $value=$this->target->$name; $this-Log("Getting Value for$name:$value"); return $value; } Public function__isset ($name){ $value=isset($this->target->$name); $this-Log("Checking isset for$name". ($value?" True ":" false ")); return $value; } Public function__call ($name,$arguments){ $this-Log("Calling Method$nameWidth: ".implode(",",$arguments)); return Call_user_func_array(Array($this->target,$name),$arguments); }}classpeople{ Public $name= ' HK '; Public $age; Public functionSayname () {return $this-name; } Public functionPlus$a,$b){ return $a+$b; }}$p=Newpeople ();$proxy=NewLoggingproxy ($p);Echo $proxy->name;//HKEcho"<br>";$proxy->age=10;Echo $p->age;//TenEcho"<br>";Echo $proxy-sayname ();Echo"<br>";Echo $proxy->plus (2,3);
Results
[Getting value for NAME:HK] HK
[Setting value for AGE:10] 10
[Calling Method Sayname width:] HK
[Calling Method plus width:2,3] 5
In most cases, a proxy should not change the behavior of the class it is acting on
Proxy is not exactly the same as the type it is acting on, which is also a disadvantage.
Therefore, if you need to type hints or code checks to make sure that the object is a specific type, you cannot use proxy mode in this case
Facade (appearance) mode provides different functions for abstracting complex functions so that applications do not need to understand the subsystem processing
The details of each request, you can complete the process
For example, when processing a typical API request, the user needs to authenticate through the subsystem, and after the authentication is passed, the request is passed through the API subsystem to the remote server
Processing, and finally the corresponding decoding by the function of the other API
The facade method is implemented as follows
<?PHPclassfacade{ Public functionApirequestjson ($method,$parameters){ $user=user::Getauthenticateduser (); if($user->haspermission ($method)){ $result=$this->api->$method($parameters); returnJson_encode ($result); } }}
Facade does not add any new functionality to the subsystem, but instead delegates the appropriate responsibilities to the subsystem.
Subsystems need not know the existence of facade, and the application does not need to know the existence of subsystems.
PHP design mode proxy (proxy mode) and facade (appearance) design mode