设计模式-模板

2018-05-07  本文已影响0人  ZjyMac

一,模板模式详解

public abstract class AbstractWork {
    protected void getUp() {
        Log.e("zjy", "起床");
    }

    protected abstract void goToWork();

    protected abstract void work();

    protected abstract void getOffWork();

    public final void newDay() {
        getUp();
        goToWork();
        work();
        getOffWork();
    }

}
public class BossWork extends AbstractWork{
    @Override
    protected void goToWork() {
        Log.e("zjy", "开车上班");
    }

    @Override
    protected void work() {
        Log.e("zjy", "分配员工");
    }

    @Override
    protected void getOffWork() {
        Log.e("zjy", "开车下班");
    }
}

public class StaffWork extends AbstractWork{
    @Override
    protected void goToWork() {
        Log.e("zjy", "公交上班");
    }

    @Override
    protected void work() {
        Log.e("zjy", "处理工作");
    }

    @Override
    protected void getOffWork() {
        Log.e("zjy", "公交下班");
    }
}
  BossWork bossWork = new BossWork();
        bossWork.newDay();
        StaffWork staffWork = new StaffWork();
        staffWork.newDay();

二,Android中的实际运用

上一篇 下一篇

猜你喜欢

热点阅读