基于事件回调操作的代码-面向接口编程

  • 事件回调操作

  • 描述:模拟界面类: 接收用户发起的事件,处理完成后显示结果

  • 需求:

  • 1.下载完成后需要显示信息

  • 2.下载过程中需要显示下载进度

  • a.该干什么事情

  • b.这个事情该怎么做

//把需要上报的事件都定义在接口中
interface INotifyCallBack{
    void progress(String file , int progress);
    void result(String file);
}
/**
 * 负责下载的类
 */
class DownLoad{

    private INotifyCallBack cb;//面向接口编程

    public DownLoad(INotifyCallBack cb){
        this.cb = cb;
    }
    /**
     * 底层执行下载的方法
     * @param file
     */
    public void start(String file){
        int count = 0;
        while (count <= 100){

            try {
                //模拟下载事件
                cb.progress(file,count);
                Thread.sleep(1000);
                count += 20;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        cb.result(file);
    }
}

public class GuiTestCase implements INotifyCallBack{

    private DownLoad downLoad;

    public GuiTestCase(){
        this.downLoad = new DownLoad(this);
    }

    /**
     * 下载文件
     * @param file
     */
    public void downLoadFile(String file){
        System.out.println("begin start download file : " + file);
        downLoad.start(file);
    }

    /**
     * 显示下载进度的方法
     * @param file
     * @param progress
     */
    public void progress(String file , int progress){
        System.out.println("download file : " + file + progress  + "%");
    }

    /**
     * 显示文件下载完成了
     * @param file
     */
    public void result(String file){
        System.out.println("download file : " + file + " over.");
    }

    public static void main(String[] args) {
        GuiTestCase gui = new GuiTestCase();
        gui.downLoadFile("视频文件");
    }
}

run。。。。
在这里插入图片描述

界面类依赖下载类:上层依赖下层 合理
下载类定义回调的接口,从而实现面向接口编程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值