Search code examples
javaservletsdesign-patternsabstract-classtemplate-method-pattern

abstract class and Template Method pattern and HttpServlet


There is no any abstract methods in the HttpServlet which is declared abstract class with key word abstract. doGet and others are not abstract methods. So why HttpServlet is declared as abstract class without any abstract class?

Is the HttpServlet an example of template method pattern?

What's the template pattern and template method pattern. What's the differences?


Solution

  • HttpServlet is declared as abstract because you are meant to subclass it. By itself, it would not provide much useful behavior, so you should extend the class and define your own behavior for one or more of the methods (most commonly, either doGet or doPost).

    Yes, HttpServlet is an example of the template method pattern. The doXXX methods in HttpServlet are called from the service method. In other words, we have a template method (service) deferring some of its processing to individual methods defined in subclasses. These methods are selected at compile time, so this is an example of the template method pattern.

    See also: