Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the agent mode of Java

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to understand Java proxy mode". In daily operation, I believe many people have doubts about how to understand Java proxy mode. Xiaobian consulted various materials and sorted out simple and easy operation methods. I hope to help you answer the doubts about "how to understand Java proxy mode"! Next, please follow the small series to learn together!

Proxy Mode: Static Proxy

Sometimes, we may not want to face something or someone directly, so we can find an intermediary to do it for us, such as sending gifts to run errands, finding cleaning in the city, etc. In this way, the other party does not know who is involved behind the intermediary, and plays an intermediary role and protects the target object. This is the proxy model. There is an agent who does it for you.

1. Definition of proxy mode

For some reason it is desirable to provide an object with a proxy to control access to that object. In this case, the access object does not fit or cannot directly reference the target object, and the proxy object acts as an intermediary between the access object and the target object.

Simple structure diagram:

2. Advantages and disadvantages of proxy mode

Advantages:

1. Play an intermediary role between the client and the target object and protect the target object

2. You can extend the functionality of the target object

3. The client can be separated from the target object, which reduces the coupling degree of the system to a certain extent and increases the extensibility of the program.

Disadvantages:

1. This increases the number of classes in the system design (because one or more mediation classes (agents) are added).

2. Adding a proxy object between the client and the target object slows down request processing

3. increases the complexity of the system.

3. Classification of proxy patterns

Proxy patterns are divided into static proxies (this article) and dynamic proxies (next article):

Static: A proxy class created by a programmer or a special tool that automatically generates source code and compiles it, and the.class file for the proxy class exists before the program runs

Dynamic: dynamically created using reflection mechanisms while the program is running

4. Structure of static proxy pattern

1. Abstract topic classes: Declare real topics and business methods implemented by proxy objects through interfaces or abstract classes

2. Real topic class: implements the concrete business in the abstract topic, is the real object represented by the proxy object, and is the object to be finally referenced.

3. Proxy class: provides the same interface as the real theme, contains references to the real theme inside, and can access, control, or extend the functionality of the real theme

Code demonstration:

Define an abstract theme:

Static proxies need to define interfaces or parent classes when used, and the proxied object implements the same interface as the proxy object or inherits the same parent class.

/** * abstract subject */public interface Subject { void Request();}

True theme:

/** */public class RealSubject implements Subject{ @Override public void Request() { System.out.println("Your flight is flying"); }}

Agent class:

//proxy class, implements abstract class interfaces public class Proxy implements Subject{ private RealSubject realSubject; @Override public void Request() { //first determine whether there is a real subject If not, create a new one. if(realSubject == null){ realSubject = new RealSubject(); } //Pretreatment, use this method preRequest(); //to access real topic classes realSubject.Request(); //successor method of proxy class postRequest(); } private void preRequest() { System.out.println("Flight is about to take off"); } private void postRequest() { System.out.println("Flight arrived at destination, thanks for taking"); }}

Test Category:

public class Test { public static void main(String[] args) { //Use proxy to access Request in RealSubject Proxy proxy = new Proxy(); proxy.Request(); }}

Output:

Flight's about to take off.

Your flight is flying.

The flight arrived at its destination. Thank you for taking the flight.

Through the above demonstration, a proxy class is used to access the method of the real subject class, so that the test class does not know who is accessing it, whether it is a proxy class or someone else behind the proxy class, so that the object can be well protected. This is also the advantage of the proxy model.

Summary of static proxies:

You can extend the functionality of the target implementation without modifying the functionality of the target object, but the proxy object needs the same interface as the target object implementation, so there will be many proxy classes. When there are many proxy classes, the maintenance complexity will become larger. How to solve this problem? Dynamic proxies are needed.

5. Application scenarios of proxy mode

When an object cannot or does not want to be referenced directly or has difficulty accessing it, it can be accessed indirectly through proxy objects. Proxy patterns are used primarily for two purposes: to protect the target object and to enhance the target object

This application scenario is summarized from the Internet:

Remote proxies, usually to hide the fact that the target object exists in a different address space for easy client access. For example, when a user requests some network disk space, a virtual hard disk will be established in the user's file system. When the user accesses the virtual hard disk, the actual access is the network disk space.

Virtual proxies, usually used when the target object to be created is expensive. For example, downloading a large image takes a long time, because some calculation is more complex and cannot be completed in a short time, then you can replace the real object with a small scale virtual agent to eliminate the user's feeling of slow server.

Security proxies, which are often used to control access to real objects by different kinds of clients

Intelligent guidance, mainly used to invoke the target object, the agent attached some extra processing functions. For example, add the ability to count references to real objects so that they are automatically released when they are not referenced

Delayed loading refers to delaying the loading of targets in order to improve system performance. For example, Hibernate has delayed loading of attributes and delayed loading of association tables

At this point, the study of "how to understand Java proxy pattern" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report