Home > Java > javaTutorial > How to understand Java's proxy mode

How to understand Java's proxy mode

PHPz
Release: 2023-04-19 11:34:05
forward
728 people have browsed it

    Agency mode: Static proxy

    Sometimes, we may not want to face something or someone directly, so we can find an intermediary Go and do things for us, such as sending gifts, calling for errands, finding a cleaner to go to a certain city, etc. In this way, the other party does not know who is involved behind the intermediary, and it plays the role of an intermediary and protects the target object. This is the agency model. That is to say, an intermediary does it for you.

    How to understand Java's proxy mode

    1. Definition of proxy mode

    For some reasons, it is necessary to provide a proxy for an object to control access to the object. At this time, the access object is not suitable 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:

    How to understand Java's proxy mode

    2. Advantages and disadvantages of the proxy model

    Advantages:

    1. Plays an intermediary role between the client and the target object. The role of protecting the target object

    2. It can expand the function of the target object

    3. It can separate the client from the target object, which reduces the coupling of the system to a certain extent and increases the program complexity. Scalability

    Disadvantages:

    1. It will cause the number of classes in the system design to increase (because there are one or more intermediary classes (agents))

    2. Adding a proxy object between the client and the target object will slow down the request processing speed

    3. Increase the complexity of the system

    3. Proxy Classification of patterns

    The proxy pattern is divided into static proxy (this article) and dynamic proxy (next article) :

    • Static: The programmer creates the proxy class or a specific tool automatically generates the source code and then compiles it. The .class file of the proxy class already exists before the program runs

    • Dynamic: Created dynamically using the reflection mechanism when the program is running

    4. The structure of the static proxy mode

    1. Abstract theme class: Declares the business methods implemented by the real theme and proxy object through interfaces or abstract classes

    2. Real theme class: Implements the business methods in the abstract theme The specific business is the real object represented by the proxy object, which is the final object to be referenced

    3. Proxy class: Provides the same interface as the real topic, and contains internal references to the real topic Reference, which can access, control or extend the functionality of the real theme

    Code demonstration:

    Define an abstract theme:

    When using a static proxy, you need to define an interface or a parent class, and the proxy object and the proxy object implement the same interface or inherit the same parent class

    /**
     *抽象主题
     */
    
    public interface Subject {
        void Request();
    }
    Copy after login

    Real topic:

    /**
     *真实主题类:实现抽象主题
     */
    public class RealSubject implements Subject{
        @Override
        public void Request() {
            System.out.println("您的航班正在飞行");
        }
    }
    Copy after login

    Agent class:

    //代理类,实现抽象类的接口
    public class Proxy implements Subject{
    
        private RealSubject realSubject;
    
        @Override
        public void Request() {
            //先判断是否存在真实主题RealSubject
            //如果没有,就新建一个
            if(realSubject == null){
                realSubject = new RealSubject();
            }
    
            //预处理,使用这个方法
            preRequest();
    
            //去访问真实主题类的方法
            realSubject.Request();
    
            //代理类的后续方法
            postRequest();
        }
    
        private void preRequest() {
            System.out.println("航班马上起飞啦");
        }
    
        private void postRequest() {
            System.out.println("航班到达目的地,感谢乘坐");
        }
    
    
    }
    Copy after login

    Test class:

    public class Test {
        public static void main(String[] args) {
    
            //使用代理去访问RealSubject里面的Request
            Proxy proxy = new Proxy();
            proxy.Request();
        }
    }
    Copy after login

    Output result:

    The flight is about to take off
    Your flight is flying
    The flight has arrived at the destination, thank you for taking the flight

    Through the above demonstration, use a proxy class to access The method of the real theme class, so that the test class does not know who is accessing it, whether it is the proxy class or there are other people behind the proxy class, so that the object can be well protected. This is also the advantage of the agency model.

    Summary of static proxy:

    You can extend the target function without modifying the function of the target object, but the proxy object needs to be the same as the target object. Interface, so there will be many proxy classes. When there are many proxy classes, the maintenance complexity will become larger. So how to solve this problem? Then you need to use dynamic proxy.

    5. Application scenarios of proxy mode

    When you cannot or do not want to directly reference an object or there are difficulties in accessing an object, you can access it indirectly through a proxy object. There are two main purposes of using the proxy mode: One is to protect the target object, and the other is to enhance the target object

    The summary of this application scenario is referenced from the Internet:

    • Remote proxy, this method is usually used to hide the fact that the target object exists in a different address space to facilitate client access. For example, when a user applies for some network disk space, a virtual hard disk will be created in the user's file system. When the user accesses the virtual hard disk, he or she actually accesses the network disk space.

    • Virtual Agent , this method is usually used when the target object to be created is expensive. For example, downloading a large image takes a long time, and some calculations are complicated and cannot be completed in a short time. In this case, you can first replace the real object with a small-scale virtual agent to eliminate the user's feeling that the server is slow

    • Security proxy, this method is usually used to control the access rights of different types of customers to real objects

    • Intelligent guidance is mainly used to add some additional processing functions to the agent when calling the target object. For example, add the function of counting the number of references to a real object, so that when the object is not referenced, it can be automatically released

    • Lazy loading refers to delaying the loading of the real object in order to improve the performance of the system. Target loading. For example, there is lazy loading of properties and lazy loading of related tables in Hibernate

    The above is the detailed content of How to understand Java's proxy mode. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template