java15 OOP静态代理设计模式 2022-02-07

public class Test1 {
    public static void main(String[] args) {
        Action userAction = new UserAction();
        ActionProxy proxy = new ActionProxy(userAction);
        proxy.doAction();
    }
}
//代理对象
class ActionProxy implements Action{
    private Action target;//被代理的对象
    public ActionProxy(Action target) {
        this.target = target;
    }

    public void doAction() {
        long startTime=System.currentTimeMillis();
        target.doAction();//执行真正的业务
        long endTime=System.currentTimeMillis();
        System.out.println("共耗时:"+(endTime-startTime));
    }
}
//业务接口
interface Action{
    public void doAction();
}
//业务具体实现类
class UserAction implements Action{
    public void doAction() {
        System.out.println("用户开始工作。。。");
    }
}

你可能感兴趣的:(java15 OOP静态代理设计模式 2022-02-07)