且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么HttpServlet类被声明为抽象?

更新时间:2022-11-23 18:13:21

这是因为它遵循模板方法设计模式。 doXxx()方法具有返回HTTP 405 方法未实现错误的所有默认行为。如果这些方法都是抽象的,那么即使您的业务需求根本不需要,您也会***全部覆盖它们。它只会导致样板代码和未指定/不直观的行为。

It's because it follows the Template Method design pattern. The doXxx() methods have all default behaviours of returning a HTTP 405 Method Not Implemented error. If those methods were all abstract, you would be forced to override them all, even though your business requirements don't need it at all. It would only result in boilerplate code and unspecified/unintuitive behaviour.