Home > Java > javaTutorial > Java static proxy and dynamic proxy in-depth learning

Java static proxy and dynamic proxy in-depth learning

黄舟
Release: 2016-12-12 13:24:50
Original
1765 people have browsed it

1. Proxy mode
The proxy mode is a commonly used Java design pattern. The characteristic is that the proxy class and the delegation class have the same interface. The proxy class is mainly responsible for preprocessing messages for the delegation class, filtering messages, forwarding messages to the delegation class, and afterwards Process messages etc.
There is usually an association between proxy classes and delegate classes. An object of a proxy class is associated with an object of a delegate class. The object of the proxy class itself does not actually implement the service, but by calling the relevant methods of the object of the delegate class. to provide specific services.
According to the creation period of the agent, the agent class can be divided into two types:
Static proxy: Created by programmers or automatically generated by specific tools and then compiled. The .class file of the proxy class already exists before the program is run.
Dynamic proxy: dynamically created using the reflection mechanism when the program is running.

2. Single static proxy

码如下:
public interface CountDao 
{ 
// 查看账户方法 
public void queryCount(); 
} 
public class CountDaoImpl implements CountDao 
{ 
public void queryCount() 
{ 
System.out.println("查看账户方法..."); 
} 
} 
public class CountTrancProxy implements CountDao 
{ 
private CountDao countDao; 
public CountProxy(CountDao countDao) 
{ 
this.countDao = countDao; 
} 
@Override 
public void queryCount() 
{ 
System.out.println("tranc start"); 
countDao.queryCount(); 
System.out.println("tranc end"); 
} 
} 
public class TestCount 
{ 
public static void main(String[] args) 
{ 
CountTrancProxy countProxy = new CountTrancProxy(new CountDaoImpl()); 
countProxy.updateCount(); 
} 
}
Copy after login

tranc start
View account method...
tranc end
3. Multiple static proxies
Added

码如下:
public class CountLogProxy implements CountDao 
{ 
private CountDao countDao; 
public CountLogProxy(CountDao countDao) 
{ 
this.countDao = countDao; 
} 
@Override 
public void queryCount() 
{ 
System.out.println("Log start"); 
countDao.queryCount(); 
System.out.println("Log end"); 
} 
}
Copy after login

Log start
before transaction processing
based on the above code View account method...
After transaction processing
Log end
4. Summary
In fact, proxy classes can be used to achieve the effect of proxy by inheritance or interface implementation. However, when multiple proxy classes need to be combined with each other, inheritance is not flexible and proxy classes need to be constantly rewritten. The way to implement interfaces is to easily combine proxy classes through aggregation.



Related labels:
source:php.cn
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