The proxy mode of the Java design pattern, and the difference from the Java callback mechanism

Source: Internet
Author: User
Tags aop ming

    1. The Java proxy pattern is :

What do you want to do to others to do (the other is the agent, he is the agent), why do you have to give others to do? If a pupil Xiao Ming, now want to write homework, but want to play games, he would like to play games, and do not want to be scolded by his mother, all he found a genius gunman, Xiao Red to his behalf to write homework, his homework to the mother to check. Xiao Ming's mother saw it was Xiao Ming's homework, and there was no doubt that he didn't do it himself. The goal is achieved, the process is not so important (of course, it is not wrong, here just to tell about the design pattern and simulate a certain scenario).

  

2. Agent Mode Highlights:

1. An abstract theme (the abstract theme here is the writing industry)

2. An agent (Xiao Hong), Inherit abstract theme (writing industry)

3. An agent (xiaoming), inherits abstract theme (writing industry)

4. A manager inherits abstract themes, a surrogate object (3 and 4 can be selected, 3 will be the agent and the manager to merge into one, 4 will be the agent and the manager separate)

      

   3. Application of Proxy mode:Spring AOP face slicing programming, such as the management of things entrusted to the spring container management, the programmer only with the core business layer, highly reusable module to the agent to do, do not repeat the work. Increase the system's code redundancy.

Code:

Abstract topics

 Package Javadesign.staticproxy; /**  */Publicinterface  proxywork   {publicvoid dohomework ();}

represented by (or delegated by)

 PackageJavadesign.staticproxy;Importorg.junit.Test;/*** Created by Administrator on 2017/12/21. * Create an agent of the manager, will be the task of the agent assigned to the real processing task of the class, that is, Xiao Ming put his homework to small redology pa do*/ Public classProxymanagerImplementsProxywork {Privateproxywork proxywork; /** This method is the method of setting proxy, applied to the idea of Java polymorphism **/     Public voidsetagency (proxywork proxywork) { This. proxywork=proxywork;//the method is to pass the real proxy class (red) as a parameter to the} @Override Public voiddohomework () {System.out.println ("Xiaoming started pretending to do his homework."); //in fact, Xiao Ming did not do homework, but called to small red, let Little red to do        if( This. proxywork!=NULL){//first of all, to determine whether the red reference passed overProxywork.dohomework ();//here, Xiao Hong began to do homework, the implementation of the key cheat plan} System.out.println ("Xiao Ming told mom, the homework is done."); }/** Start test agent to simulate a scene * **/@Test Public voidClient () {Proxymanager Proxymanager=NewProxymanager (); Proxymanager.setagency (NewProxyrealwork ());//new Proxyrealwork Create a proxy named Little RedProxymanager.dohomework ();//Xiao Ming began to pretend to do homework, really do homework is little red    }}

Agent:

 PackageJavadesign.staticproxy;/*** Created by Administrator on 2017/12/21.*/ Public classProxyrealworkImplementsProxywork {PrivateProxywork proxywork;//will be the agent, that is, the game small Wang Zixiao pass over, in fact, is the Java polymorphism//in this case, the proxy is used for polymorphic     Public voidSetagency (Proxywork proxywork) {//The instantiated xiaoming will be passed in here.         This. proxywork=proxywork; }    /*This agent is the person who really does the homework, also is the small red this genius*/@Override Public voiddohomework () { This. before (); //This position is the location service layer equivalent to the spring Aop tangent pointSystem.out.println ("The real task performer, is performing the task, that is, little red doing homework."));  This. After (); }    /** Preprocessing and aftercare are similar to spring's aspect-oriented programming, with the implementation of transaction management, logging, etc. **/    //preprocessing Actions    Private voidbefore () {//TODOSystem.out.println ("Xiao Hong has studied his homework and started doing her homework.")); }    //Aftercare Treatment    Private voidAfter () {//TODOSystem.out.println ("Little Red to finish the homework again checked again, can not do all right, otherwise too false"); }}

The difference between proxy mode and callback mode:

I think the two are the same, are the same kind of thinking (their own things to others to do, themselves issued an order), to really find a difference, that from their specific implementation mode (Agent model: the pursuit of unity of the theme, issued orders, each to seek their own things; callback mechanism: Pursuit of notification, issue orders, accept notifications)

Note: The proxy mode in this article is static proxy, the coupling is very high, it may be seldom used in the project, so it is necessary to use the reflection mechanism to realize the dynamic proxy in the project to develop the general agent. (for example, Xiao Red to help Xiao Ming, Xiao Zhang, small Wang San personal writing industry, their workbooks to correspond to their three people, can not only correspond to a person)

Note: This article is original, can be reproduced, can be commented, there is no place to welcome correct, in other places to see similar articles, do not like to spray, just a deeper understanding of their own written articles.

  

The proxy mode of the Java design pattern, and the difference from the Java callback mechanism

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.