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
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
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
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!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software