package org.design;
public class ProxyDemo {
public static void main(String[] args) {
Network network = null;
network = new Proxy(new Real());
network.browse();
}
}
interface Network{
public void browse();
}
class Real implements Network{
@Override
public void browse() {
// TODO Auto-generated method stub
System.out.println("surfing in Internet");
}
}
class Proxy implements Network{
private Network network;
public Proxy(Network network) {
// TODO Auto-generated constructor stub
this.network = network;
}
@Override
public void browse() {
// TODO Auto-generated method stub
this.check();
this.network.browse();
}
public boolean check(){
//拦截操作
System.out.println("未成年禁止上网");
return false;
}
}
接口定义类,实例化实现类
代理类去构造实现类,然后实现的前后做相关的操作
spring的aop操作就是利用代理模式实现的