Java's Spring (AOP) Prelude-Dynamic proxy design pattern (top)

Source: Internet
Author: User
Tags aop

We often encounter this kind of thing, the project manager lets you add a function A to a function class, then you work overtime to add the function A to this class;

It's been two days. New requirements, and then add a new function after a function B, you work overtime to write this function B, added to a behind;

A few days later, the project manager said that the two new functional order needs to be replaced, then you find a week before the source code, read the

Think about how it was realized at that time, this part is function A, that part is function B, then Balabala Change Order. See if this is

Feel very annoying, and will be in the source code to add a new function, and then in the source code to change the order of new functions, then a large Tuo source code, fortunately, since

Write the source code, understand quickly, operation is also very quickly, in case someone else's source code, other people's programming habits may be different from their own,

Look at his source code, but also spend a little more time, really pack up and leave the heart has.

At this time, we can use the agent to order the source code to add new features.

1. Define an interface

 Public Interface Interfacedo {    publicvoid  dosomething ();}

2. Define the original function class

 Public class Implements Interfacedo {    publicvoid  dosomething () {        System.out.println ( "What to do--------");}    }

We can see that this class implements the previous interface, but only one thing: thinking about life!

3. Define additional function classes and implement calls to the original functionality

 Public class Implements Interfacedo {    private  Interfacedo todo;      Public impeat (Interfacedo todo) {        Super();         this. Todo = todo;    }      Public void dosomething () {        todo.dosomething ();        System.out.println ("I'm going to eat--------");}    }

We see that this class introduces a member variable, type Interfacedo (interface) type,

The constructor implementation assigns a value to the member variable, and when we instantiate the class, the member variable inside it points to the instance of the passed-in Parameter object.

Persontodo a = new Persontodo ();

Impeat B = New Impeat (a);

So, b.dosomething () This method implements the function is: first performs the instantiation time passes in the parameter Todo's todo.dosomething () method, then

Then execute the statement System.out.println ("I'm going to eat------------");

In the above example, since the parameter passed in is a, then b.dosomething () is equal to the following two steps:

①a.dosomething ();

②system.out. println ("I'm going to eat------------");

Results Console printout:

What are you going to do--------

I'm going to eat,---------.

After we figure this out, we're going to create a new Impeat class, which also implements the Interfacedo interface.

 Public class Implements Interfacedo {    private  Interfacedo todo;      Public impgame (Interfacedo todo) {        Super();         this. Todo = todo;    }      Public void dosomething () {        System.out.println ("I'm going to play a game--------");        Todo.dosomething ();    }}

The parameters of the above interface implementation class with the parameter constructor are the Interfacedo (interface) type, so the parameter can be an instantiated object for all implementation classes of the interface.

4. Application of polymorphism

 Public class Testperson {    publicstaticvoid  main (string[] args) {        new  Persontodo ();         New Impeat (a);         New Impgame (c);         New Impsleep (d);        B.dosomething ();    }}

We can see this test class, passing the instantiated object of the original function class as a parameter to the Impeat-containing constructor, and then instantiating the Impeat object

As a parameter to the Impgame-containing constructor, and then the Impgame instantiation of the object as a parameter passed to the Impsleep-containing constructor, and finally called the

Impgame the DoSomething () method of the instantiated object.

So, in fact, this method is:

The DoSomething () method of the parameter is called first and then a statement is printed, and the DoSomething () method of the parameter is also

The DoSomething () method of the parameter is called first and then a statement is printed, so the loop can be represented by a graph.

-----------------------------------------------------------------------------

-----------------------------------------------------------------------------

Each box is a class, of course, the new function order can be freely selected, you can also implement the function 2 in the implementation of 1.

As a result, each feature class other than the original feature class is capable of adding functionality to other functional classes.

Since all of the above classes implement the Interfacedo interface, you can rewrite the above code:

 Public class Testperson {    publicstaticvoid  main (string[] args) {        new  Persontodo ();         New Impeat (a);         New Impgame (c);         New Impsleep (d);        B.dosomething ();    }}

It is not difficult to see that this is a reference to a polymorphic, interface reference to an implementation class object.

C added function on the basis of the function of A;

D added function on the basis of C function;

B adds functionality based on the function of D.

This implements the static proxy.

Java's Spring (AOP) Prelude-Dynamic proxy design pattern (top)

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.