Statement: This series of blog reference "Big Talk design mode", author Geoscience.
Proxy Mode provides a proxy for other objects to control access to this object. In some cases, an object does not fit or cannot directly reference another object, and a proxy object can act as an intermediary between the client and the target object .
UML Class Diagrams:
Role Description:
Abstract Theme Role ( Igivegift ): defines the Follower and the Proxy public interface, so that it can be used in any Follower can be used in all areas Proxy .
Theme Role ( Follower ): defines the Proxy The real entity that is represented.
Proxy Object ( Proxy ): Saving a reference allows the agent to access the entity and provides a Follower Interfaces with the same interface, so that proxies can be used instead of entities (Follower) .
Code implementation:
<?php/** * Created by Phpstorm. * user:lyl * DATE:2015/5/16 * time:16:33 *//** top-level interface * Interface igivegift */interface igivegift{function giverose (); function givechocolate ();} /**-seekers * Class Follower */class Follower implements igivegift{private $girlName; function __construct ($name = ' Girl ') {$this->girlname= $name; } function Giverose () {echo ' {$this->girlname}: This is the rose I sent you, I hope you can like it. <br/> "; } function Givechocolate () {echo ' {$this->girlname}: This is the chocolate I sent you, I hope you can accept it. <br/> "; }}/** Proxy * Class proxy */class proxy implements igivegift{private $follower; function __construct ($name = ' Girl ') {$this->follower=new follower ($name); } function Giverose () {$this->follower->giverose (); } function Givechocolate () {$this->follower->givechocolate (); }}
Client code:
Header ("Content-type:text/html;charset=utf-8");//------------------------proxy mode test code------------------require_ Once "./proxy/proxy.php"; $proxy =new Proxy (' Fan Bingbing '); $proxy->giverose (); $proxy->givechocolate ();
Advantages:
1 , Clear Responsibilities
The real role is to realize the actual business logic, do not care about other non-responsibility transactions, through the late agent to complete a complete transaction, the accompanying result is simple and clear programming.
2 , the proxy object can be used between the client and the target object to the role of intermediaries, This acts as a mediator and protects the target audience.
3 , High scalability
Applicable scenarios:
1) The remote proxyprovides a local proxy object for an object that is located in a different address space.
This different address space can be in the same host, but also in another host, the remote agent is also called Ambassador (Ambassador)
2) virtual proxiescreate expensive objects as needed.
If you need to create an object that consumes a large resource, first create an object that consumes relatively little, and the real object is created only when it is needed.
3) The protection agent (Protectionproxy) Controls access to the original object.
The protection agent is used when the object should have different access rights.
4) smartReferencereplaces a simple pointer that performs some additional operations when accessing an object.
5) Copy-on-write Agent: It is a virtual agent, the replication (cloning) operation is deferred to only when the client really needs to execute.
In general, deep cloning of objects is a costly operation, Copy-on-write The agent can delay this operation and is cloned only when the object is used.
PHP Object-oriented design pattern
PHP design mode-proxy mode