Home > Java > javaTutorial > An example analysis of the Java proxy pattern.

An example analysis of the Java proxy pattern.

PHPz
Release: 2023-05-09 17:07:33
forward
1214 people have browsed it

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();
    }
}
Copy after login

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!

Related labels:
source:yisu.com
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