Reprint Please specify source: http://blog.csdn.net/l1028386804/article/details/45568963
I. Overview
Provides a proxy for other objects to control access to this object.
Second, applicability
1. The remote Agent (REMOTEPROXY) provides a local representation of an object in different address spaces.
2. Virtual agents (Virtualproxy) create expensive objects as needed.
3. The protection agent (Protectionproxy) Controls access to the original object. 4. The Smart Guide (smartreference) replaces the simple pointer, which performs some additional operations when accessing an object.
Third, participants
1.Proxy saves a reference so that the agent can access the entity. If the interface of Realsubject and subject is the same, proxy references subject. Provides an interface that is the same as the subject interface, so that proxies can be used instead of entities. Controls access to the entity and may be responsible for creating and deleting it. Other features depend on the type of proxy:
2.RemoteProxy is responsible for encoding the request and its parameters and sending the encoded request to entities in different address spaces.
3.VirtualProxy can cache additional information for an entity in order to delay access to it.
4.ProtectionProxy checks whether the caller has the necessary access rights to implement a request.
5.Subject defines the common interface for realsubject and proxies, so that proxies can be used wherever realsubject is used.
6.RealSubject defines the entity represented by proxy
Four, class diagram
V. Examples
Proxy
Package com.lyz.design.proxy;/** * Proxy * @author Liuyazhuang * */public class Proxyobject implements Object { Ob Ject obj; Public Proxyobject () { System.out.println ("This is the proxy class"); obj = new Objectimpl (); } public void Action () { System.out.println ("Agent Start"); Obj.action (); SYSTEM.OUT.PRINTLN ("proxy End");} }
Subject
Package com.lyz.design.proxy;/** * Subject * @author Liuyazhuang * */public interface Object { void action ();}
Realsubject
Package com.lyz.design.proxy;/** * Realsubject * @author Liuyazhuang * */public class Objectimpl implements Object {
public void Action () { System.out.println ("========"); System.out.println ("========"); System.out.println ("This is the class being represented"); System.out.println ("========"); System.out.println ("========");} }
Test
Package com.lyz.design.proxy;/** * Test * @author Liuyazhuang * */public class Test {public static void main () {
object obj = new Proxyobject (); Obj.action (); }}
Result
This is the proxy class proxy start ================ which is the proxy class ================ proxy end
A brief talk on the Java design mode--agent mode (proxy)