Spring Static Proxy

Source: Internet
Author: User

first, the concept of Agent
Provides a proxy for an object to control access to the object. The proxy class and the delegate class have a common parent or parent interface, which can be overridden by proxy objects wherever a delegate class object is used. The proxy class is responsible for the preprocessing of the request, filtering, assigning the request to the delegate class processing, and subsequent processing after the delegate class finishes executing the request.

Two. Proxy mode

As can be seen, the Proxy interface (Subject), the proxy class (Proxysubject), the delegate class (Realsubject) Form a "product" word structure.
Depending on the generation time of the proxy class, the agent can be divided into static agent and dynamic agent two kinds.

The following is a simulation requirement for static and dynamic proxies: The delegate class is dealing with a lengthy task, and the customer class needs to print out the time it takes to execute the task. To solve this problem, you need to record the time before and after the task is executed, and the two time difference is how long the task executes.

second, static agent
The source code of the proxy class is created by the programmer or tool, and then the proxy class is compiled. The so-called Static is in the program before the existence of the proxy class of bytecode files, the relationship between the proxy class and the delegate class is determined before running.

Next, let's take a look at the static proxy case

Define an interface class first

 Package aop005;  Public Interface Girl {    publicvoid  KFC (String datetime);      Public void Meet (String datetime);}

Then define two classes separately

 Packageaop005;/* *  */ Public classGirl1Implementsgirl{ Public voidKFC (String datetime) {System.out.println ("[core business logic] I'm the first girl."); System.out.println ("[Core business logic]" +datetime+ "Eat KFC"); }         Public voidMeet (String datetime) {System.out.println ("[core business logic] I'm the first girl."); System.out.println ("[Core business logic]" +datetime+ "Appointment"); }}
 Packageaop005;/* *  */ Public classGirl2ImplementsGirl { Public voidKFC (String datetime) {System.out.println ("[core business logic] I'm a second girl."); System.out.println ("[Core business logic]" +datetime+ "Eat KFC"); }         Public voidMeet (String datetime) {System.out.println ("[core business logic] I'm a second girl."); System.out.println ("[Core business logic]" +datetime+ "Appointment"); }}

Then we build a static proxy class

 Packageaop005;/** This example is static proxy * 1, broker and want star, must implement the same interface * 2, the star as a property of this class, used to invoke*/ Public classGirlproxyImplementsGirl {PrivateGirl G;  Publicgirlproxy (String name) {if("GIRL1". Equals (name)) {g=NewGIRL1 (); }Else if("Girl2". Equals (name)) {g=NewGirl2 (); }} @Override Public voidKFC (String datetime) {System.out.println (Bath); System.out.println (Makeup); System.out.println ("Get dressed."); System.out.println ("*****************");                G.KFC (datetime); System.out.println ("*****************"); System.out.println (Remover); System.out.println (Bath); System.out.println (Sleep); } @Override Public voidMeet (String datetime) {System.out.println (Bath); System.out.println (Makeup); System.out.println ("Get dressed."); System.out.println ("*****************");                G.meet (datetime); System.out.println ("*****************"); System.out.println (Remover); System.out.println (Bath); System.out.println (Sleep); }}

Then we'll get a test class.

 Packageaop005;/** Add a "Static proxy class", similar to the star of the broker * The core business logic of the code and non-core separation * The non-core code to the broker to manage, * Note: Brokers and to star, must implement the same interface * * Disadvantage: * 1. Every point needs to be written Once * 2. When the detached non-core code, if the need to modify, then the whole will be changed*/ Public classTest { Public Static voidMain (string[] args) {Girl G1=NewGirlproxy ("Girl1"); Girl G2=NewGirlproxy ("Girl2"); G1. KFC ("Saturday"); G1.meet ("Sunday"); G2. KFC ("Saturday"); G2.meet ("Sunday"); }}

The test results are as follows:

Spring Static Proxy

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.