Static proxy
1. Role Analysis of static agents (UML drawing recommends using STARUML software)
Abstract roles---generally implemented using interfaces or abstract classes.
It's really the role---being represented.
Proxy role---Proxy real role-the agent is usually a subordinate operation after the role.
The customer-------Use the proxy role for some action.
2. Implementation of the code (landlord-intermediary-customer)
rent.java--Abstract Roles
Package Cn.sxt.staticproxy;public interface Rent {public void Rent ();
host.java--Real Characters
Package Cn.sxt.staticproxy;public class Host implements rent{@Overridepublic void Rent () {System.out.println ("House Rental");}
proxy.java--Agent Role
Package Cn.sxt.staticproxy;public class Proxy implements Rent{private host Host;public proxy () {}public proxy (host host) { Super (); this.host = host;} public void Sethost (host host) {this.host = host;} Rental public void rent () {seehouse (); host.rent (); Fare ();} See Room private void Seehouse () {System.out.println ("with guest viewing room");} Agency fee private void fare () {System.out.println ("Charge intermediary fee");}}
client.java--Customer
Package Cn.sxt.staticproxy;public class Client {public static void main (string[] args) {host host=new host (); Proxy proxy=new Proxy (host);p roxy.rent ();}}
3. Benefits of using static proxies:
Make real character processing business more pure, no longer pay attention to some public things;
The public business by the agent to complete---to achieve the division of business;
When the public service expands, it becomes more centralized and convenient;
To illustrate:
Userservice.java:
Public interface UserService {public void Add ();p ublic void Update ();p ublic void Delete ();p ublic void Search ();
Userserviceimpl.java: Focus on pure business logic
public class Userserviceimpl implements UserService {@Overridepublic void Add () {System.out.println ("Add User");} @Overridepublic void Update () {System.out.println ("Modify user");} @Overridepublic void Delete () {System.out.println ("delete user");} @Overridepublic void Search () {System.out.println ("query User");}}
Userserviceproxy.java: Handling Some public transactions, such as logs
public class Userserviceproxy implements UserService {private userservice userservice; @Overridepublic void Add () {log (" Add "); Userservice.add ();} @Overridepublic void Update () {log ("Update"); Userservice.update ();} @Overridepublic void Delete () {log ("delete"); Userservice.delete ();} @Overridepublic void Search () {log ("search"); Userservice.delete ();} public void log (String methodName) {System.out.println ("execute" + methodName + "execute");}}
Disadvantages of static proxies:
Class has become more---More proxy class, the workload has become larger, the development efficiency has been reduced
As a----dynamic agents appear: The advantages of static agents, the disadvantage of discarding static agents
Dynamic Agent
1. The role of dynamic agents and static proxies is the same.
2. The proxy class for dynamic proxies is dynamically generated.
3. Dynamic agents are divided into two categories
A) interface-based dynamic agent---JDK dynamic agent
B) Class-based dynamic proxy---Cglib
Now use Javasist to generate dynamic proxies
4.JDK Dynamic Proxy---proxy class and Invocationhandler interface
InvocationHandler
Is the interface that is implemented by the calling handler for the proxy instance.
Each proxy instance has an associated call handler. When a method is called on a proxy instance, it encodes the method call and assigns it to its method of calling the handler invoke
.
Invoke method:
Object |
invoke(Object proxy, Method method, Object[] args) Processes the method call on the proxy instance and returns the result. |
Processes the method call on the proxy instance and returns the result. This method is called on the calling handler when the method is called on the proxy instance associated with the method.
-
Parameters:
-
proxy
-the proxy instance on which the method is invoked
-
method
-corresponds to an instance of an interface method that is called on the proxy instance
Method
. The
Method
declaration class for an object will be the interface in which the method is declared, which can be a hyper-interface to the proxy interface on which the proxy class inherits the method.
-
args
-An array of objects that contains the parameter values of the method calls on the incoming proxy instance, or if the interface method does not use parameters
null
. The parameters of the base type are wrapped in an instance of the appropriate basic wrapper class (such as
java.lang.Integer
or
java.lang.Boolean
).
-
Return:
The
-
value returned from the method call of the proxy instance. If the declaration return type of an interface method is a base type, the value returned by this method must be an instance of the corresponding base wrapper object class, otherwise it must be a type that can be assigned to the declaration return type. If this method returns a value of
null
and the return type of the interface method is the base type, the method call on the proxy instance is thrown
NullPointerException
. Otherwise, if the value returned by this method is incompatible with the declared return type of the interface method above, the method call on the proxy instance will be thrown
ClassCastException
.
Proxy
Provides a static method for creating dynamic proxy classes and instances, or a superclass of all dynamic proxy classes created by these methods.
static Object |
newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) Returns a proxy class instance for the specified interface that can assign a method call to the specified invocation handler. |
Returns a proxy class instance for the specified interface that can assign a method call to the specified invocation handler. This method is equivalent to:
Proxy.getproxyclass (loader, interfaces). GetConstructor (new class[] {invocationhandler.class}).
Proxy.newProxyInstance
Thrown IllegalArgumentException
, with the Proxy.getProxyClass
same reason.
-
Parameters:
-
loader
-The class loader that defines the proxy class
-
interfaces
-the interface list to be implemented by the proxy class
-
h
-The call handler that assigns the method call
Realize:
public class Proxyinvocationhandler implements invocationhandler{//target Object--Real object private objects Target;public void Settarget (Object target) {this.target = target;} /** * Generate proxy class: */public Object GetProxy () {return proxy.newproxyinstance (This.getclass (). getClassLoader (), Target.getclass (). Getinterfaces (), this);} /** * proxy--is a method object called by the proxy class * method--proxy class instance (proxy) call handler (ADD) * For example, call Proxy.add (): Here method.getname is add */@Overridepublic Object Invoke (Object proxy, Method method, object[] args) throws Throwable {log (Method.getname ()); Object result= Method.invoke (target, args); return result;} public void log (String methodName) {System.out.println ("Execute" +methodname+ "method");} See Room private void Seehouse () {System.out.println ("with guest viewing room");} Agency fee private void fare () {System.out.println ("Charge intermediary fee");}}
Client:
Import Cn.sxt.service.userservice;import Cn.sxt.service.userserviceimpl;public class Client {public static void main ( String[] args) {userservice userservice=new userserviceimpl (); Proxyinvocationhandler pih=new Proxyinvocationhandler ();p ih.settarget (UserService); UserService proxy= (UserService) pih.getproxy ();p roxy.add ();}}
A dynamic agent General agent of a certain class of business, a dynamic agent can be agents of many types of
Spring study notes--agent