Can someone explain how javax.servlet.http.HttpServlet methods like service(),doGet() and doPost() can be based on strategy pattern?

huangapple go评论68阅读模式
英文:

Can someone explain how javax.servlet.http.HttpServlet methods like service(),doGet() and doPost() can be based on strategy pattern?

问题

javax.servlet.http.HttpServlet的service()方法,以及所有接受HttpServletRequest和HttpServletResponse对象作为参数的doXXX()方法,据说都是基于策略模式的。我对此不太清楚。如果有人能帮助解释一下,那就太好了。

英文:

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?

答案1

得分: 1

这是在指涉默认的service实现将检查HTTP方法(HttpServletRequest对象的属性),然后调用相应的doX方法,该方法默认情况下返回一些“未实现”HTTP错误(除了doHead,如果我记得正确的话,它会调用doGet)。

像“策略模式”这样的术语没有固定的定义。是否认为这是“策略模式”的好例子取决于定义该术语的人。这不同于比如在Java中“public”一词的意义(在这里有一个明确的规定:它的意义正是Java语言规范所说的。不多也不少。对于类似“策略模式”之类的术语,没有这样的规范,这是我的观点)。

Servlet的API相当糟糕,我绝对不会尝试将其用作制作API的示例。我会看看像Jersey这样的东西。

英文:

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.

答案2

得分: 1

根据维基百科,策略模式(https://en.wikipedia.org/wiki/Strategy_pattern)"允许在运行时选择算法"。

因此,如果您查看过滤器(https://docs.oracle.com/javaee/6/api/javax/servlet/Filter.html),它们实现了一个doFilter方法,这可以视为"算法"。可以有ACL过滤器、命中计数器过滤器、GZIP过滤器等等。

这些过滤器根据请求参数和服务器配置在运行时应用,所以我猜您可以提出这是策略模式的一个示例。

英文:

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.

答案3

得分: 1

根据GoF对策略模式的定义,这是一个糟糕的示例。策略对象不是调用它的方法的方法参数。策略模式是基于组合的,而在HttpServlet中明显没有组合关系。

此外,HttpServletRequestHttpServletResponse参数不是“算法族”。它们是数据承载者。

最后,HttpServlet不是“替代子类化”的选择。它是一个_抽象_类,因此专门设计用于继承。

这个糟糕的示例在Examples of GoF Design Patterns in Java's core libraries中重复出现。不幸的是,在线上关于设计模式的误导信息是猖獗的

英文:

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.

huangapple
  • 本文由 发表于 2020年8月10日 01:06:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63329138.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定