mercredi 12 août 2020

Template Method: How to use it when you want to be able to flexibly change primitive options?

The problem is best explained with the following code:

public class TemplateClass {
    public void templateOne() {
        checkConditionA();
        primitiveOp1();
        checkConditionB();
    }

    public void templateTwo() {
        checkConditionA();
        primitiveOp2();
        checkConditionB();
    }

    protected abstract void primitiveOp1();
    protected abstract void primitiveOp2();
    // rest of the methods
}

  

Now I have code duplication with templateOne() and templateTwo(), but I would like to have just one template method but with interchangeable primitive operations.

Aucun commentaire:

Enregistrer un commentaire