Proxy pattern in Java
Proxy is a design pattern that allows an object to appear in the form of another object and access the original object through the proxy object. Proxies are widely used in many applications, the most common of which is to implement remote object calls and logging in the network.
There are also many examples of using the proxy pattern in Java. The proxy mode in Java is mainly implemented in the following three ways:
Static proxy is implemented by creating a proxy class during the compilation phase. The proxy class and the original class implement the same interface, and the proxy class implements its own methods by calling the methods of the original class. The advantage of static proxy is that it is relatively simple and intuitive, but the disadvantage is that the proxy class needs to be written manually. When the proxy method is added or modified, the proxy class needs to be modified accordingly.
The following is a simple static proxy example:
// 定义接口 interface UserManager { void add(); void delete(); } // 原始类 class UserManagerImpl implements UserManager { public void add() { System.out.println("添加用户"); } public void delete() { System.out.println("删除用户"); } } // 代理类 class UserManagerProxy implements UserManager { private UserManager userManager; public UserManagerProxy(UserManager userManager) { this.userManager = userManager; } public void add() { System.out.println("记录日志..."); userManager.add(); System.out.println("记录日志..."); } public void delete() { System.out.println("记录日志..."); userManager.delete(); System.out.println("记录日志..."); } } // 客户端代码 UserManager userManager = new UserManagerImpl(); UserManagerProxy userManagerProxy = new UserManagerProxy(userManager); userManagerProxy.add(); userManagerProxy.delete();
Dynamic proxy creates a proxy class through the reflection mechanism at runtime. Unlike static proxies, dynamic proxies can proxy multiple interfaces and do not need to manually write proxy classes. The advantage of dynamic proxy is that it is more flexible, but the disadvantage is that it has a certain impact on performance.
The following is a simple dynamic proxy example:
// 定义接口 interface UserManager { void add(); void delete(); } // 原始类 class UserManagerImpl implements UserManager { public void add() { System.out.println("添加用户"); } public void delete() { System.out.println("删除用户"); } } // 代理类 class MyInvocationHandler implements InvocationHandler { private UserManager userManager; public MyInvocationHandler(UserManager userManager) { this.userManager = userManager; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("记录日志..."); Object result = method.invoke(userManager, args); System.out.println("记录日志..."); return result; } } // 客户端代码 UserManager userManager = new UserManagerImpl(); InvocationHandler handler = new MyInvocationHandler(userManager); UserManager proxy = (UserManager) Proxy.newProxyInstance( userManager.getClass().getClassLoader(), userManager.getClass().getInterfaces(), handler ); proxy.add(); proxy.delete();
CGLIB proxy is implemented by generating a subclass of the original class. CGLIB proxies can proxy classes that do not implement interfaces and are faster than dynamic proxies. However, the CGLIB proxy also has the disadvantage that it requires that the original class cannot be final and that all methods must not be final.
The following is a simple CGLIB proxy example:
// 原始类 class UserManager { public void add() { System.out.println("添加用户"); } public void delete() { System.out.println("删除用户"); } } // 代理类 class MyMethodInterceptor implements MethodInterceptor { private UserManager userManager; public MyMethodInterceptor(UserManager userManager) { this.userManager = userManager; } public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { System.out.println("记录日志..."); Object result = method.invoke(userManager, args); System.out.println("记录日志..."); return result; } } // 客户端代码 UserManager userManager = new UserManager(); Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(UserManager.class); enhancer.setCallback(new MyMethodInterceptor(userManager)); UserManager proxy = (UserManager) enhancer.create(); proxy.add(); proxy.delete();
Summary
The proxy pattern is a commonly used design pattern that can be used for It provides additional functionality. In Java, there are three ways to implement the proxy pattern: static proxy, dynamic proxy and CGLIB proxy. Different implementation methods have their own advantages and disadvantages, and can be selected and used according to specific circumstances.
The above is the detailed content of Proxy pattern in Java. For more information, please follow other related articles on the PHP Chinese website!