Template Method Design Pattern in Java? Example Tutorial

Template Method Pattern defines a sequence of steps of an algorithm but let you free to implement those steps on your way. You cannot change the algorithm but you can define how those steps should behave. To make sure that you cannot change the algorithm, template methods are usually made final in Java, which is also a good use case of when to make a method final in Java. The subclasses are still  allowed to override the steps but as I said, they are not allowed to change the sequence. It can also be provided a default implementation for one or more steps. Let us look at the following example to understand Template Method Pattern.  A Template method pattern provides a skeleton for performing any sort of algorithm or an operation, and it allows the sub-classes to re-define part of the logic

Post a Comment