Proxy mode static proxy, dynamic proxy

Proxy pattern , a commonly used design pattern.


Advantages:
1. The client side indirectly controls another object RealSubject by using the proxy object ProxySubject , so that the client side is decoupled from the specific implementation class RealSubject , and the scalability is better; 2. The client side does not need to care about the method implementation of the specific implementation class RealSubject , only the methods provided in the relationship proxy object are needed; 3. The proxy ProxySubject also serves the purpose of controlling security access.

Disadvantages:
1. Due to the addition of proxy objects, the request processing speed may be slowed down;
2. Additional work is required to implement the proxy mode. The same method needs to be implemented on both the proxy object and the real implementation object. Increase The amount of code is reduced, and some proxy modes are more complicated to implement;

Note: Here you may think that the proxy mode is similar to the decorator mode, but in fact the proxy mode is to control access to the real object RealSubject; and the decorator mode is to enhance the function of the real object RealSubject.

Take a look at the UML diagram:

preview

Static proxy:

1. Create a Game interface

public interface Game{
	 void play(String gameName);
}

2. Create an entity class RealGame that implements the interface

public class RealGame implements Game{
        @Override
        public void play(String gameName) {
            System.out.println("play " + gameName);
        }
}

3. Create a proxy class ProxyGame that implements the interface

public class ProxyGame implements Game{

        private RealGame realGame;

        public ProxyGame(){
            realGame = new RealGame();
        }

        @Override
        public void play(String gameName) {
            realGame.play(gameName);
        }
}

4. The client client uses the proxy class ProxyGame to obtain the object of the RealGame class

public class ProxyGameDemo {
   
    public static void main(String[] args) {
        Game game = new ProxyGame();
        game.play("静态代理 game");
    }
}

5. Execute the program and output the result:

play 静态代理 game

Dynamic proxy:

1. Create a Game interface

public interface Game{
     void play(String gameName);
}

2. Create an entity class RealGame that implements the interface

public class RealGame implements Game{
        @Override
        public void play(String gameName) {
            System.out.println("play " + gameName);
        }
}

3. Create a ProxyGameHandler to implement the InvocationHandler interface method

public class ProxyGameHandler implements InvocationHandler {
        protected Game game;

        public ProxyGameHandler(Game game) {
            this.game = game;
        }

        /**
         *
         * @param proxy 被代理的对象
         * @param method 要调用被代理对象的方法
         * @param args   方法调用时需要的参数
         * @return
         * @throws Throwable
         */
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            return method.invoke(game, args);
        }
}

4. The client client uses the proxy class ProxyGame to obtain the object of the RealGame class

public class ProxyGameDemo {
   
    public static void main(String[] args) {
        RealGame realGame = new RealGame();
        Game game = (Game) Proxy.newProxyInstance(
                realGame.getClass().getClassLoader(),  //被代理类的类加载器
                realGame.getClass().getInterfaces(),   //被代理类的接口方法
                new ProxyGameHandler(realGame));       //拦截的方法在被拦截时需要执行哪个InvocationHandler的invoke方法
        game.play("动态代理 game");
    }
}

5. Execute the program and output the result:

play 动态代理 game


The choice of which proxy mode to use depends on the actual situation of the project. If the above content is helpful to you, please remember to like it three times~

Guess you like

Origin blog.csdn.net/qq_33539839/article/details/113560312