The target object is accessed through the proxy object, which increases the additional operations on the target object to achieve the purpose of extending the target object functionality, such as spring transactions, AOP, and so on.
IMPORTANT: 1) abstract Role: A business method that declares a real role through an interface or abstract class. (move in the example)
2) Proxy roles: Implementing abstract roles, acting as proxies for real roles (*proxy in the example)
3) Real role: Implement abstract roles, define the business logic to be implemented by the real role for the proxy role to invoke. (Moveimpl in the example)
First, static agent
1. Inherit and implement static proxy
Mobile interface
Public interface Move {public void Move ();}
Mobile Implementation Classes
Public class Moveproxy implements move {public void Move () { System.out.println ("Car is running ...");} }
Car Agent Class
public class Carproxy extends moveimpl { @Override public void Move () { long start_time = new Date (). GetTime (); System.out.println ("The car starts running ..."); Super. Move (); Long end_time = new Date (). GetTime (); SYSTEM.OUT.PRINTLN ("Car end Run, run time:" + (End_time- start_time));}}
Test
Public class Model { publicstaticvoid main (string[] args) { New carproxy (); Carproxy.move (); }}
2, aggregation to implement static proxy
It is recommended to use aggregation to implement dynamic proxy, so it is convenient to implement multiple agent sequential exchange, which is easy to expand, as follows
Mobile interface
Public interface Move {public void move ();}
Mobile Implementation Classes
public class Moveproxy implements move {public void Move () { System.out.println ("Car is running ..."); c8/>}}
Car Agent Class
Public class extends Moveimpl { @Override publicvoid Move () { longNew Date (). GetTime (); System.out.println ("The car starts running ..."); Super . Move (); Long New Date (). GetTime (); System.out.println ("Car end Run, run time:" + (End_time- start_time));} }
Log Agent
Public class Implements Move { private move obj; Public logproxy (Move obj) { this. obj = obj; } Public void Move () { System.out.println ("log start ..."); Obj.move (); SYSTEM.OUT.PRINTLN ("Log end ..."); }
Test
Public classModel { Public Static voidMain (string[] args) {//It is recommended to use aggregation to implement dynamic proxy, so it is convenient to implement multiple agent sequential exchange, which is easy to expand, as followsMoveimpl Moveproxy =NewMoveimpl (); Carpropxy Carpropxy=NewCarpropxy (Moveproxy); Logproxy Logproxy=NewLogproxy (CARPROPXY); Logproxy.move (); System.out.println (); Moveimpl moveProxy1=NewMoveimpl (); Carpropxy carPropxy1=NewCarpropxy (MOVEPROXY1); Logproxy logProxy1=NewLogproxy (CARPROPXY1); Logproxy1.move (); }}
Second, dynamic agent
Mobile interface
Public interface Move {public void Move ();}
Mobile Implementation Classes
Public class Moveproxy implements move {public void Move () { System.out.println ("Car is running ...");} }
Log Agent
Public classLoghandlerImplementsInvocationhandler {PrivateObject obj; Publicloghandler (Object obj) { This. obj =obj; } PublicObject Invoke (Object proxy, Method method, object[] args)throwsthrowable {System.out.println ("Log Start ..."); Method.invoke (obj); System.out.println ("Log End ..."); return NULL; }}
Time Agent
Public classTimehandlerImplementsInvocationhandler {PrivateObject obj; Publictimehandler (Object obj) { This. obj =obj; } PublicObject Invoke (Object proxy, Method method, object[] args)throwsThrowable {LongStart_time =NewDate (). GetTime (); System.out.println ("The car started running ..."); Method.invoke (obj); LongEnd_time =NewDate (). GetTime (); System.out.println ("Car end Run, run time:" + (End_time-start_time)); return NULL; }}
Test
Public classModel { Public Static voidMain (string[] args) {Moveimpl move=NewMoveimpl (); Class C=Move.getclass (); Timehandler T=NewTimehandler (move); Move m=(Move) proxy.newproxyinstance (C.getclassloader (), c.getinterfaces (), t); Loghandler L=NewLoghandler (m); Move M1=(Move) proxy.newproxyinstance (C.getclassloader (), C.getinterfaces (), L); M1.move (); }}
The difference: Java learning-static proxy vs dynamic Agent
Java design mode (iv) proxy mode