Java and Design Patterns-proxy mode

Source: Internet
Author: User

The proxy mode, also known as the delegate mode, is a structured design pattern that provides a proxy for other objects to control access to the object. It sounds difficult to understand, the life of the agent is a lot of examples, graduation to find a house, how fast and good to find their favorite house, must through the rental agent; want to buy train tickets, railway station too far, we can choose agent point to buy tickets. The UML class diagram for code patterns is as follows:

Scenario: When there is no or no direct access to an object or access to an object can be accessed indirectly through a proxy object, in order to ensure the transparency of client use, the delegate object and the proxy object need to implement the same interface. Below we take "tears lawsuit" as an example, explain the proxy mode. We all know that we have to ask for a lawyer in a lawsuit, and this lawyer can think of us as our agent, and we do something through him.

First abstract class, lawsuit:

Package Com.proxy.demo;public Abstract class Lawsuit {/** * abstract classes */abstract void lawsuit ();}

Very simple, an abstract method, the following implementation of the tears class, that is, the real class, overwrite the implementation of this abstract method:

Package Com.proxy.demo;public class Qiuju extends lawsuit {/** * Real class */@Overridevoid lawsuit () {System.out.println ("I am the chrysanthemum, I want a lawsuit! ");}}

Here is the proxy class:

Package Com.proxy.demo;public class Proxysubject {/** * proxy class */qiuju qiuju=null;public proxysubject (Qiuju QiuJu) {//Through construction method Introduce the This.qiuju=qiuju of the Chrysanthemum object;} public void lawsuit () {qiuju.lawsuit ();//agent for the tears of the lawsuit}}

The proxy class actually did nothing, just called the real class method. The following test class:

Package Com.proxy.demo;public class TestClass {public static void main (string[] args) {Qiuju qiuju=new qiuju (); Proxysubject proxysubject=new proxysubject (Qiuju);p roxysubject.lawsuit ();//Call method by proxy}}

To run the test method:

As we can see, the proxy invokes the real class method, which is implemented smoothly. Here by the way, adjust the console font size method:

Open the window-preferences--general-appearance-colors and Fonts--debug-console font to adjust

Proxy mode is a very common method, careful friends will find that our last section of the article "strategy mode" in the use of proxy mode. The proxy mode can be divided into two parts, one is static agent and the other is dynamic agent. This is the static proxy, the agent's code is generated by the programmer itself or through some automated tools to generate fixed code and then compile it, that is, before our code is running, the class compiler file of the proxy classes already exist, while the dynamic code through the reflection mechanism to dynamically generate the agent's object, The agent who will decide at the execution stage. Java also provides us with a handy dynamic proxy interface Invocationhandler that implements this interface by overriding its invocation method invoke. In other words, first we need to refine this dynamic proxy class:

Package Com.proxy.demo;import Java.lang.reflect.invocationhandler;import Java.lang.reflect.method;public class Dynamicproxy implements Invocationhandler {object Object=null;public Dynamicproxy (Object object) {This.object = object;} @Overridepublic object Invoke (Object proxy, Method method, object[] args) throws Throwable {Object  result= Method.invoke (object, args); return result;}}

Interface Invocationhandler: Only one method Object:invoke is defined in this interface (Object Obj,method method, J2eejava language jdk1.4apijavalangobject.html ">object[] args). When actually used, the first parameter, obj, generally refers to the proxy class, which method is the proxy, as in the previous example, the request (), args is the parameter array for the method. This abstract method is implemented dynamically in the proxy class.  
The original in the agent class to deal with things to Invocationhandler to deal with, no longer care about who the end agent.

Modify the test class below:

Package Com.proxy.demo;import Java.lang.reflect.proxy;public class TestClass {public static void main (string[] args) {/* * Qiuju qiuju=new Qiuju (); Proxysubject proxysubject=new * Proxysubject (Qiuju); Proxysubject.lawsuit ();//proxy method */lawsuit lawsuit = new Qiuju ();D ynamicproxy dynamicproxy = new Dynamicproxy (lawsuit) ; ClassLoader loader = Lawsuit.getclass (). getClassLoader ();//Gets the classloader//dynamic construction of the proxy class A lawyer object lawsuit lawyer = (lawsuit ) proxy.newproxyinstance (loader, new class[] {lawsuit.class}, Dynamicproxy); Lawyer.lawsuit ();}}


Note that, due to the need of the dynamic proxy, the lawsuit becomes an interface, and the Qiuju class becomes the implementation interface.

Run the instance with the same results as the static proxy:

It can be seen that the dynamic proxy class can proxy n proxy class, its essence decoupling the proxy class and the proxy class, so that there is no direct decoupling relationship between the two, which is more flexible when used. Static proxies are more consistent with object-oriented thinking and can be implemented as needed when developing.




Java and Design Patterns-proxy mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.