Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Template Method pattern and HttpServlet

 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Day,

The book "Sun Certified Enterprise Architect for J2EE, Study Guide" by Paul Allen and Joseph Bambara says the following on page #269:

The Template Method pattern's intent is to define the skeleton of a function in an operation, deferring some steps to its subclasses. The Template Method lets subclasses redefine certain steps of a function without changing the structure of the function. The HttpServlet does this in the servlet API.


I'm not sure I understand the last sentence:

The HttpServlet does this in the servlet API.


Any ideas?

Thanks,
Dan
 
James Wang
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dan,

There are doPost(),doGet() and others in HttpServlet class, but there is not any code in the body of these methods, so the doPost(),doGet are only defined as a skeleton, but when a servlet is defined, generally, the HttpServlet is extended and the doPost(),doGet() or other methods are implemented, so it satisfy the template pattern rule.
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James,

The documentation about HttpServlet says:

Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site.


A definition of abstract class is:

A class must be declared abstract if it has one or more methods declared abstract.


I guess that in the case of HttpServlet, it is declared abstract even though it has no abstract methods; by doing it, it becomes a template.

Regards,
Dan
 
U Patel
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by James Wang:
Hi Dan,

There are doPost(),doGet() and others in HttpServlet class, but there is not any code in the body of these methods, so the doPost(),doGet are only defined as a skeleton, but when a servlet is defined, generally, the HttpServlet is extended and the doPost(),doGet() or other methods are implemented, so it satisfy the template pattern rule.


I thought rule to have a template method is to control to flow of a certain process where you can define a method that eventually calls certain abstract methods which are implimented by subclass. I thought service() method is the template method in servlet which eventually ends up calling appropriate doXXX method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic