Java Proxy mode-static proxy mode

Source: Internet
Author: User

One: Proxy mode

The role of proxy mode is to provide a proxy for other objects to control access to this object. 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.

Second: The role of proxy mode design

abstract role : Declares a common interface (abstract class or interface) between objects and proxy objects.

Proxy Role : The proxy object role contains a reference to a real object that can manipulate the true object, while the proxy object provides the same interface as the real object so that at any moment it can replace the real object. At the same time, the proxy object can attach other actions when performing the operation of the real object, which is equivalent to encapsulating the true object.

Real role : The real object represented by the proxy role is the object we end up referencing.

Three: one case demonstration

Abstract Role: (abstract Class)

Real characters:

Agent role:

Client:


IV: The second case demonstration

Abstract role: (interface)

Real characters:

Agent role:

additional action classes for the agent role:

Client code:

If we use spring to do this, we need to make the following changes:

To add a spring configuration file:

Client code:

Five: Summarize and generalize

Case Demo One:

If you want to use proxy mode in the way described above (static proxy), then the real role must be the implementation already exists and as the internal property of the proxy object.

  But when actually used, a real role must correspond to a proxy role, but if a large number of uses can lead to the rapid expansion of the class, in addition, if you do not know the real role, how to use the proxy? This question can be solved by using the Dynamic proxy class of Java (One of my directories "JDK dynamic agent and Cglib dynamic agent will talk about").

Case Demo II:

Disadvantages of static proxies:  

1. If there are 100 DAO, create 100 proxy objects.

2. If there are many methods in a DAO that require transactions, there are many duplicate codes in the methods in the proxy object.

3. By 1th and second point can be drawn: Static proxy reusability is not strong.

Of course, static proxies also have their own somewhat, such as code enhancements, and so on.

1. The static proxy class and the delegate class implement the same interface, and the proxy class implements the same method through the delegate class. There is a lot of code duplication. If the interface adds a method, all the proxy classes need to implement this method, in addition to all implementation classes that need to implement the Square method. Increases the complexity of code maintenance.  

2. Static proxy objects serve only one type of object, if you want to serve multiple types of objects. It is bound to be a proxy for each object, static agent in the program is a little larger than the capacity of the.

Java Proxy mode-static proxy mode

Related Article

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.