Does empty method break the structure of template method pattern?

516 views Asked by At

In template method pattern, we have a superclass which is defined abstract. And we have concrete classes that extend this class.

Assume that we add a new function to template method and override it in some of subclasses(only for using in some classes). We override this new method in other classes as empty(function has no body).

Does this operation count as code duplication?

enter image description here

For example, in template method we have a new function doA() (I tought to use doA() as a hook method). I want to use this function only in Class1 and between doZ() and doY().

If I add this function to template method and override this function in Class2 empty. Does this count as code duplication?

public void template(){
    doX();
    doY();
    doZ();
}

public void template(){
    doX();
    doY();
    doA();
    doZ();
}
1

There are 1 answers

0
Rui On

Definitely not. You can find from the book gof23 that there are two types of methods in template method pattern:

  • abstract method, which does not have implementation body but waits for the subclass to override it
  • hook method, which is just an empty method, meaning you can just implement it in case of need, but it is not necessary