Defines a proxy for other objects to control access to this object
Class diagram
Generic Code Abstract topic class public interface Subject {public void request ();} Real topic class Public classes Realsubject implements Subject {@Overri De public void request () {//Specific business process}} proxy class public class Proxy implements Subject {//To delegate which implementation class private Subject Subject = n ull; Default by proxy public proxy () {this.subject = new proxy (),////through constructor pass proxy public Proxy (Object...args) {}//Implement the method defined in the interface @Override public void request () {This.before (); This.subject.request (); This.after ();}//preprocessing private void before () {}//Aftercare Priv ate void After () {}}
Advantages
1. The role of clear and real roles is the business logic of implementation, do not care about other not to blame the transaction
2. Highly scalable specific theme roles can not be changed, as long as it implements the interface, the proxy class can be used without any modification of the case
3. Intelligent Dynamic Proxy is the best embodiment, that is, in the runtime to specify the actual proxy object (struts how to map the form elements to the object?)
Use scenario typical applications like spring AOP
Extended
1. The general agent requires the client to access only the proxy role, but not the real role
2. Force the proxy to require the client to find the agent role through the real role (actor and broker)
3. Virtual agent refers to the need to initialize the subject object, to avoid too much of the proxy object caused by the slow initialization of the public class Proxy implements subject{Private Subject Subject p ublic void Request () {If (subject==null) {subject = new Realsubject ();} subject.request ();}}
1. Dynamic Proxy refers to the implementation phase does not care who agents, and in the runtime to specify the agent which object, dynamic agent based on the interface of the agent to generate all the method is given an interface, dynamic geography on the publicity I have implemented all the methods under the interface (through the Invocationhandler interface)
Class diagram
Source code 1. Interface public interface Igameplayer {public void login (String user,string password), public void Killboss (), public void UPGR Ade (); } 2. Implementation class public class Gameplayer implements Igameplayer {private String name= ' "; public gameplayer (String _name) {This.name=_n Ame @Override public void Killboss () {System.out.println (this.name+ "in Play)"} @Override public void login (String user, Strin G password) {System.out.println ("User with login name" +user+ "+this.name+ login succeeded"); @Override public void Upgrade () {System.out.pri Ntln (this.name+ "another Level"); } 3. Dynamic proxy class public class Gameplayih implements Invocationhandler {//proxy class CLS =null;//proxy instance Object obj=null;//I want to delegate who pub Lic Gameplayih (Object _obj) {this.obj=_obj;}//Calling the method of the proxy @Override public object Invoke (object proxy, methods, OBJEC T[] args) throws Throwable {Object result =method.invoke (this.obj, args); if (Method.getname (). Equalsignorecase ("Login" ) {System.out.println ("Someone is logged in");} else if (Method.getname (). Equalsignorecase ("Upgrade")) {System. OUT.PRINTLN ("someone upgraded"); return result; } 4. Scene class public class Client {public static void main (string[] args) {Igameplayer player = new Gameplayer ("Guo Jun"); Invocationhandler handler = new Gameplayih (player); System.out.println ("Start playing the game, note the timestamp:" +system.currenttimemillis ()); ClassLoader C1=player.getclass (). getClassLoader (); Dynamically generate a proxy class Igameplayer proxy = (Igameplayer) proxy.newproxyinstance (c1, New Class[]{igameplayer.class}, Handler); Proxy.login ("Guojun", "password"); Proxy.killboss (); Proxy.upgrade (); System.out.println ("End time is:" +system.currenttimemillis ()); } }