I read somewhere that javax.servlet.http.HttpServlet: service() method, plus all of the doXXX() methods that accept HttpServletRequest and HttpServletResponse objects as arguments. are based on a strategy pattern. I am not clear about this. It will be great if someone could help?
-
Where did you read this? Please provide a link to the source. – jaco0646 Aug 09 '20 at 21:47
-
https://refactoring.guru/design-patterns/strategy/java/example – prachi julka Aug 10 '20 at 08:10
3 Answers
It's referring to the notion that the default implementation of service
will check the HTTP method (a property of the HttpServletRequest
object), and will then just call the appropriate doX method, which, by default, returns some 'not implemented' HTTP error (except doHead
, which, if I recall correctly, invokes doGet).
There is no set definition of terms like 'strategy pattern'. Whether you feel this is a nice example of 'the strategy pattern' is up to whomever is defining the word. This isn't like, say, the meaning of the word 'public' in java (where there is a dictate: It means precisely what the Java Language Specification says it means. No more. No less. There is no such specification for things like 'the strategy pattern', is my point).
The API of servlets is quite bad, I definitely wouldn't try to use that as some sort of example of one should make APIs. I'd look at things like Jersey.

- 85,357
- 5
- 51
- 72
-
The specification for the Strategy Pattern is in the [Gang of Four book](https://www.amazon.com/dp/0201633612/). – jaco0646 Aug 09 '20 at 21:44
-
That would be the 'gang of four's strategy pattern'. Just like python's "for" and java's "for" have different meanings, and yet both use the same word, you'd need to specify that. I haven't (yet) seen the decree that GoF has legal claim to the term. – rzwitserloot Aug 10 '20 at 13:43
-
I suppose the meaning of all terms is debatable. Certain definitions of HTTP and API from Urban Dictionary would greatly alter the meaning of this answer. Are legal citations necessary for future readers to understand this answer? – jaco0646 Aug 10 '20 at 14:31
-
If the community at large is almost entirely in agreement on what 'Strategy Pattern' would mean, by all means, let's just go with the GoF's exact definition. I'd say that for the term 'HTTP', the vast majority of the community _would_ agree; there is no need to link to the RFC for clarity. – rzwitserloot Aug 10 '20 at 16:03
According to Wikipedia, the Strategy Pattern (https://en.wikipedia.org/wiki/Strategy_pattern) "enables selecting an algorithm at runtime".
So if you look at Filters (https://docs.oracle.com/javaee/6/api/javax/servlet/Filter.html) they implement a doFilter method which would be the "algorithm". There can be ACL Filters, Hit counter filters, GZIP filters, etc...
The filters are applied at runtime depending on request parameters and server configuration, so I guess you could make the argument that this is an example of the Strategy Pattern.

- 744
- 1
- 4
- 15
Based on the GoF definition of the Strategy Pattern, that is a bad example. A strategy object is not a method argument to the method which invokes it. The Strategy Pattern is based on composition, and clearly there is no composition relationship in HttpServlet
.
Furthermore, the HttpServletRequest
and HttpServletResponse
parameters are not "families of algorithms." They are data carriers.
And finally, HttpServlet
is not "an alternative to subclassing". It is an abstract class, so it is specifically designed for inheritance.
This poor example is repeated in Examples of GoF Design Patterns in Java's core libraries. Unfortunately, misinformation regarding design patterns is rampant online.

- 15,303
- 7
- 59
- 83