1. Classification
Proxies in Java are divided into static proxies and dynamic proxies according to the timing of proxy class generation. Static proxy classes are generated during compilation, while dynamic proxy classes are dynamically generated during Java runtime. Dynamic agents include JDK agents and CGLib agents.
2. Proxy instance
public class HelloWorld { public static void main(String[] args) { ProxyPoint pp = new ProxyPoint(); pp.sell(); } } // 卖票接口 interface SellTickets { void sell(); } // 火车站:火车站具有卖票功能,所以需要实现SellTickets接口 class TrainStation implements SellTickets { @Override public void sell() { System.out.println("火车站卖票"); } } // 代售点 class ProxyPoint implements SellTickets { private TrainStation station = new TrainStation(); @Override public void sell() { System.out.println("代售点收起一些服务费用"); station.sell(); } }
The above is the detailed content of An example analysis of the Java proxy pattern.. For more information, please follow other related articles on the PHP Chinese website!