模板方法模式

2018-03-02  本文已影响7人  863cda997e42

模板方法模式


public abstract class AbstractClass {

    protected abstract void doSomething();
    protected abstract void doOthers();
    
    public void templateMethod(){
        this.doSomething();
        this.doOthers();
    }
}

public class ConcreteClass1 extends AbstractClass{
    
    protected void doSomething() {
        // TODO Auto-generated method stub
        
    }
    
    protected void doOthers() {
        // TODO Auto-generated method stub
        
    }

}

public class ConcreteClass2 extends AbstractClass {

    protected void doSomething() {
        // TODO Auto-generated method stub

    }

    protected void doOthers() {
        // TODO Auto-generated method stub

    }

}


public class Client {
    
    public static void main(String[] args){
        AbstractClass class1 = new ConcreteClass1();
        AbstractClass class2 = new ConcreteClass2();
        class1.templateMethod();
        class2.templateMethod();        
    }
}



上一篇下一篇

猜你喜欢

热点阅读