mercredi 6 février 2019

Template pattern but with one of the classes no implementing the method

I am trying to implement Template method pattern, but I need a slight variation that I don't think is best practice. I have the following structure of classes

abstract class AbsClass {
    public void algorithm(){
        step1();
        step2();
    }
    private void step1() {
        //implementation
    }

    protected abstract void step2();
}

class A extends AbsClass {
    protected void step2() {
        // With implementation
    }
}

class B extends AbsClass {
    protected void step2() {
        // No implementation needed
    }
}

In the real case I have like 4 classes, and one of them doesn't need to have implementation for the second step. I don't think to leave the method empty would be good practice. I was thinking to put a comment(saying there is no need for implementation) in it, but I don't this would be the right solution. Is there another approach I am not seeing?

Aucun commentaire:

Enregistrer un commentaire