設計模式之模板方法模式

csdnerM發表於2020-10-24
public abstract class BankTest {
    public static void main(String[] args) {
        DrawMoney drawMoney = new DrawMoney();
        drawMoney.dothing();
        ManagerMoney managerMoney = new ManagerMoney();
        managerMoney.dothing();


    }

    public void havaNumber(){
        System.out.println("取號辦理業務");
    }

    /**
     * 鉤子方法,具體實施業務操作
     */
    public abstract void transact();
    public void talk(){
        System.out.println("給剛才的服務打個分\n");
    }

    /**
     * final限制不可更改執行順序
     */
    public final void dothing(){
        havaNumber();
        transact();
        talk();
    }
}
class DrawMoney extends BankTest{
    @Override
    public void transact() {
        System.out.println("我要存款");
    }
}
class ManagerMoney extends BankTest{

    @Override
    public void transact() {
        System.out.println("我要理財");
    }
}

相關文章