7. Dynamic Agent
A proxy is one of the basic design patterns that you insert to replace an "actual" object in order to provide additional or different operations. These operations typically involve communication with an "actual" object, so the agent usually acts as a middleman.
What is proxy mode?
The role of proxy mode is to provide a proxy for other objects to control access to this object.
What are the benefits of proxy mode?
In some cases, one customer does not want or cannot refer to another object directly, whereas a proxy object can act as an intermediary between the client and the target object.
The agent model generally involves the following roles:
abstract Role : a common interface that declares real objects and proxy objects, so that proxy objects can be used wherever a target object can be used.
Proxy Role : The proxy object contains a reference to the target object, so that the target object can be manipulated at any time, and the proxy object provides the same interface as the target object so that the target object can be substituted at any time. Proxy objects typically perform an operation before or after a client call is passed to the target object, rather than simply passing the call to the target object, while the proxy object can attach additional actions when performing the real object operation, which is equivalent to encapsulating the real object.
Real Role : Defines the target object represented by the proxy object, the real object represented by the proxy role, the object that we end up referencing, and defines the target object that the proxy object represents.
Public InterfaceAbstractobject { Public voiddosomething ();} Public classRealobjectImplementsAbstractobject { Public voiddosomething () {System.out.println ("DoSomething"); }} Public classProxyobjectImplementsabstractobject{PrivateAbstractobject Abstractobject; Publicproxyobject (Abstractobject abstractobject) { This. Abstractobject =Abstractobject; } Public voiddosomething () {System.out.println ("Proxy dosomething"); Abstractobject.dosomething (); }} Public classPorxydemo { Public Static voidMain (string[] args) {NewProxyobject (NewRealobject ()). DoSomething (); }}
The dynamic agent of Java is a step ahead of the idea of the agent, which is generated dynamically when the program is running.
Advantages of dynamic agents
The advantage of dynamic proxies is that the functions of the proxy class can be handled uniformly, without modifying each function of the proxy class.
When using dynamic proxies, we need to define a mediation class that is located between the proxy class and the delegate class, which is required to implement the Invocationhandler interface, which is defined as follows:
Public Interface Invocationhandler { publicthrows throwable;}
When we invoke the method of the proxy class object, this call is forwarded to the Invoke method, the proxy class object is passed in as a proxy parameter, and the parameter method identifies which method of the proxy class we specifically call, and args is the parameter of this method. In this way, our invocation of all methods in the proxy class becomes a call to invoke, so that we can add uniform processing logic to the Invoke method (or different proxy class methods depending on the method parameter). We call the Newproxyinstance method of the proxy class to obtain an instance of the proxy class that implements the specified interface and distributes the method call to the specified calling processor.
Public InterfaceAbstractobject { Public voiddosomething (); Public voiddootherthing ();} Public classRealobjectImplementsAbstractobject { Public voiddosomething () {System.out.println ("DoSomething"); } Public voiddootherthing () {System.out.println ("Dootherthing"); }} Public classDynamicproxyhandlerImplementsinvocationhandler{PrivateAbstractobject Abstractobject; PublicDynamicproxyhandler (Abstractobject abstractobject) { This. Abstractobject =Abstractobject; } Publicobject Invoke (Object proxy, Method method, object[] args)throwsthrowable {System.out.println ("Before"); Object result=Method.invoke (Abstractobject, args); System.out.println ("After"); returnresult; }} Public classDynamicproxydemo { Public Static voidMain (string[] args) {Dynamicproxyhandler Dynamicproxyhandler=NewDynamicproxyhandler (Newrealobject ()); Abstractobject Proxy= (abstractobject) proxy.newproxyinstance (abstractobject.class. getClassLoader (),NewClass[]{abstractobject.class}, Dynamicproxyhandler); Proxy.dosomething (); Proxy.dootherthing (); }}
Dynamic Agent Summary
First, the proxy class instance is obtained through the Newproxyinstance method, and then the proxy class method is invoked through the proxy class instance, which is forwarded to the Invoke method of the mediation class, and in the Invoke method we invoke the corresponding method of the delegate class, and we can add our own processing logic.
Java programming thought reading notes--14th chapter type Information