当前位置:网站首页>Agent model of structured model

Agent model of structured model

2022-06-22 19:34:00 OldZhangYH

The proxy pattern

An object A Need to give an object B Provide a proxy to control the object A The interview of . At this time, access the object C Does not fit or cannot directly reference the target object , So you need a proxy object A As an access object C And accessed objects B The intermediary of .( Like buying a computer , We all bought them from the dealers in computer city , It is impossible to get goods directly from Asustek's factory ).
There are two modes of agency :

  1. Static proxy : The proxy class of the static proxy is generated during the compilation of the program .
  2. A dynamic proxy : The proxy class of dynamic proxy will be generated dynamically when the program is running .

structure

  • Abstract theme class (Subject): Declare the business logic implemented by real topics and proxy objects through interfaces or abstract classes .
  • Real topic class (Real Subject): Implements the methods of the abstract topic class , Is the real object represented by the proxy object , Is the object that is eventually referenced
  • proxy class (Proxy): Provides the same interface as the real topic , There are internal references to real topics . You can visit 、 Control and extend the functionality of real topics

Static proxy

Asustek, a computer manufacturer, sells it to the dealer computer city at the ex factory price , Then the computer city expanded the selling function to increase the price 2000 Sell to the consumer .
 Insert picture description here

A dynamic proxy

In the way of dynamic proxy , Proxy classes are generated automatically during the running process of a program . So we can't find a proxy class in the file Java Of documents . We used a ProxyFactory Class to generate the proxy class . it is to be noted that ProxyFactory Not a proxy class , Instead, it is a factory that produces proxy classes , A real proxy class will only be generated in memory during runtime .
JDK Provides Proxy Class newProxyInstance() Method to return the proxy object . This method has three parameters :

  1. ClassLoader loader, Class loader for real topic classes
  2. Class<?>[] interfaces, The interface implemented by the class of the real topic class
  3. InvocationHandler, The method to be called by the proxy object

among InvocationHandler The implementation subclass of the interface represents the business logic of the proxy object , He needs to rewrite invoke Method also has three parameters :

  1. proxy Proxy object , Is the proxy object finally returned .
  2. method Reflected method objects
  3. args Call the parameters of the method
public class ProxyFactory {
    private Asus asus = new Asus();
    public ComputerOrz getProxyObj() {
        ComputerOrz proxyObj = (ComputerOrz) Proxy.newProxyInstance(
                asus.getClass().getClassLoader(),
                asus.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        method.invoke(asus, args);
                        System.out.println(" Computer city markup 2000 Sell to consumers ( A dynamic proxy )");
                        return null;
                    }
                });
        return proxyObj;
    }
}

 Insert picture description here

The difference between dynamic agent and static agent

  1. Java All dynamic proxies are proxies for interfaces , That is to say, if you do not define an abstract topic class, you cannot use a dynamic proxy . Dynamic proxy centralizes all the methods in the interface into invoke() in , In this way, it can be handled flexibly when there are many methods in the interface . The static proxy needs a method by method transfer .
  2. When a method is added to the interface , Static proxy all implementation classes and proxy classes must implement this method . Dynamic proxy has no proxy class at all , We just need to modify it invoke() Inside a part of the code can .
  3. The proxy class of the static proxy is generated during the compilation of the program , The proxy class of dynamic proxy will be generated dynamically when the program is running .

Advantages and disadvantages

advantage :

  1. The proxy pattern acts as a mediator and protector between the client and the target object
  2. The proxy object can extend the function of the target object
  3. The proxy pattern separates the target object from the client , The coupling degree is reduced to some extent

shortcoming :

  • The complexity of the system has increased

Use scenarios

  1. Remote agent
    When connecting to a remote server locally , To realize network communication . But the network communication part 2 hidden , Only one interface is given to the local service , Don't care too much about the details of the communication part .
  2. Firewall proxy
    When the browser uses a proxy , The firewall will forward the browser's request to the Internet . Internet response , The proxy server is loading the corresponding to the browser
  3. Protection agency
    Control access to objects , Different levels of permissions can be provided to different users as required .

Source code

原网站

版权声明
本文为[OldZhangYH]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221758470504.html