代理模式

来源:互联网 发布:mysql 查看表锁情况 编辑:程序博客网 时间:2024/06/06 19:03
//代理模式:使用代售点的方法代理火车站的方法:在代理类中使用代理方法,实例化一个被代理的对象,并调用被代理的类方法。public class ProxyPattern {    public static void main(String[] args) {        //实例化代售点类对象        SellingPoint s = new SellingPoint();        //调用售票点的方法,代售点的方法里已包含火车站的方法        s.maipiao();    }}//定义接口sell,并创建统一方法maipiaointerface sell{    public void maipiao();}//定义火车站类实现接口sellclass TrainStation implements sell{    @Override    public void maipiao() {        System.out.println("TrainStation is selling");    }}//定义售票点类实现接口sellclass SellingPoint implements sell{    //定义private一个火车站类型的t    private TrainStation t;    @Override    public void maipiao() {        //判断并实例化火车站的对象t        if(t == null){            t = new TrainStation();         }        BeforeSelling();        //调用火车站的买票方法        t.maipiao();        AfterSelled();    }    private void BeforeSelling(){        System.out.println("Log:Selling start");    }    private void AfterSelled(){        System.out.println("Log:Selling finished");    }}
0 0
原创粉丝点击