这个类不应该是抽象的吗?如果我直接使用它,反正会给我默认对象,因此我必须继承它并覆盖我关心的方法。我使用它的方式如下:为什么HttpServletRequestWrapper类不是抽象的
public class HttpRequestWrapper extends HttpServletRequestWrapper {
private static final Logger logger = LoggerFactory
.getLogger(HttpRequestWrapper.class);
private final byte[] body;
private static Gson gson = new Gson();
public HttpRequestWrapper(HttpServletRequest request) throws IOException {
super(request);
// Read the request body and save it as a byte array
InputStream is = super.getInputStream();
body = IOUtils.toByteArray(is);
}
@Override
public ServletInputStream getInputStream() throws IOException {
return new ServletInputStreamImpl(new ByteArrayInputStream(body));
}
@Override
public BufferedReader getReader() throws IOException {
String encBody = getCharacterEncoding();
if (encBody != null && encBody != enc) {
enc = encBody;
}
return new BufferedReader(new InputStreamReader(getInputStream(), enc));
}
xxxxxxxx more code xxxxxx
}
我不明白,直接创造的了HttpServletRequestWrapper因此为什么不是抽象对象的优势在哪里?我错过了一些关键的见解吗?包装(这是我在做什么)的包装是多余的?
更新1:有没有其他我可以用来实现相同的库?
实际上,此类的默认行为是回退到默认实现,并通过将其作为抽象来实现同样的行为。可以定义一个抽象类并提供所有方法的实现。抽象的好处是它不会在开发人员的心目中产生任何疑问,因为它可以直接用作包装类。 – Richeek