java - Calling the sub class method from super class isn't best practice? -


i working on project have abstract class(baseconverter) 1 abstract method(convert()) , few concrete methods. 1 important concrete method invokeconverter() call convert() method implemented in subclass.

while our code being reviewed other guy, told that, subclass methods shouldn't called superclass , told not best practice. below our class structure. can please tell whether isn't right way do?

@named public abstract class baseconverter{        @inject      private conversiondriver conversiondriver;//this class responsible return correct subclass object based on type       protected abstract string convert(object toconvert);        public string invokeconverter(convertertype type, object toconvert){        conversiondriver.getconverter(type).convert(toconvert);//getconverter() return subclass object based on type      }      ....     .... } 

it design pattern called template method gof. however, should not on apply favors inheritance on composition.

define skeleton of algorithm in operation, deferring steps subclasses. template method lets subclasses redefine steps of algorithm without changing algorithm's structure.

you'll find pattern implemented in many known frameworks , plugins. but, should consider different patterns strategy or decorator in cases.

for instance, while strategy pattern uses delegation vary whole algorithm, template method uses defamed inheritance vary specific part of algorithm. strategy modifies logic of individual objects @ run-time, while template method modifies logic of entire class @ compile-time subclassing.


regarding "best practice" - controversial term. believe template method should replaced in favor of better design pattern when code base grows , refactoring better complexity needed.

but best solution needs. example, might have doexecute() method , other programmers extend class (whilst not modifying it), let them hook parts of code providing beforeexecute() method. mature system maybe include event dispatcher capabilities if talked combination of various objects.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -