[英]Effective Java item 16 (2nd edition) - Is Forwarding class only used to allow re-use?
I am going through Effective Java, Item-16 Favor composition over inheritance
. 我正在通过
Effective Java, Item-16 Favor composition over inheritance
。 I looked at the Forwarding class
example below. 我查看了下面的
Forwarding class
示例。
I am wondering what's the point of having a ForwardingSet
class? 我想知道有一个
ForwardingSet
类是什么意思? InstrumentedSet
can very well implement Set
and have a private instance of it which invokes all the methods. InstrumentedSet
可以很好地实现Set
并拥有一个调用所有方法的私有实例。
Is it to promote re-use and prevent redundancy if we end up having more InstrumentedSet
like classes in the future which just need to do something in addition to the base behavior? 是否可以促进重用并防止冗余,如果我们最终在将来需要除了基本行为之外还需要做一些类型的
InstrumentedSet
? Is it just future-proofing the design or is there something else to it that I am missing? 它只是面向未来的设计,还是我缺少的其他东西?
// Reusable forwarding class
public class ForwardingSet<E> implements Set<E> {
private final Set<E> s;
public ForwardingSet(Set<E> s) { this.s = s; }
public void clear() { s.clear(); }
public boolean contains(Object o) { return s.contains(o); }
...
}
// Wrapper class - uses composition in place of inheritance
public class InstrumentedSet<E> extends ForwardingSet<E> {
private int addCount = 0;
public InstrumentedSet(Set<E> s) { super(s); }
@Override public boolean add(E e) {
addCount++;
return super.add(e);
}
...
}
ForwardingSet
is a framework. ForwardingSet
是一个框架。 If you have to write several Set
s that work with other Set
s internally but provide different functionalities on top of the "vanilla" Set
, you'd better write the common part once and not several times. 如果你有写一些
Set
,与其他加工对象物S Set
小号内部的“香草”的顶部,但提供不同的功能Set
,你最好几次写的共同部分一次不行。
Joshua Bloch, in Effective Java refers to it as "composition", though the actual implementation looks more like the decorator pattern . 有效Java中的 Joshua Bloch将其称为“组合”,尽管实际实现看起来更像装饰器模式 。
An actual implementation is readily available in Guava , as a class named ForwardingSet
. 作为名为
ForwardingSet
的类, Guava中可以使用实际的实现。
Is it to promote re-use and prevent redundancy if we end up having more InstrumentedSet like classes in the future which just need to do something in addition to the base behavior?
是否可以促进重用并防止冗余,如果我们最终在将来需要除了基本行为之外还需要做一些类型的InstrumentedSet?
Yes. 是。
Is it just future-proofing the design?
它只是面向未来的设计吗?
Yes. 是。
or is there something else to it that I am missing?
还是我还缺少其他的东西?
No, you're not missing anything. 不,你没有遗漏任何东西。
Is it to promote re-use and prevent redundancy? 是促进重复使用并防止冗余吗? Yes.
是。
Is it just future-proofing the design? 它只是面向未来的设计吗? Yes.
是。
Is there something else to it that I am missing? 我还缺少其他东西吗? No.
没有。
The "forwarding" is commonly referred to as " delegation ". “转发”通常被称为“ 委托 ”。
See: What is the purpose of a delegation pattern? 请参阅: 委派模式的目的是什么?
Some examples of Java classes with delegate-only implementations: 具有委托实现的Java类的一些示例:
java.io.FilterInputStream
(and java.io.FilterOutputStream
, java.io.FilterReader
, java.io.FilterWriter
) java.io.FilterInputStream
(和java.io.FilterOutputStream
, java.io.FilterReader
, java.io.FilterWriter
)
The class
FilterInputStream
itself simply overrides all methods ofInputStream
with versions that pass all requests to the contained input stream.FilterInputStream
类本身简单地覆盖了InputStream
所有方法,其中的版本将所有请求传递给包含的输入流。 Subclasses ofFilterInputStream
may further override some of these methods and may also provide additional methods and fields.FilterInputStream
子类可以进一步覆盖其中一些方法,还可以提供其他方法和字段。
javax.servlet.ServletRequestWrapper
(and javax.servlet.ServletResponseWrapper
, javax.servlet.http.HttpServletRequestWrapper
, javax.servlet.http.HttpServletResponseWrapper
) javax.servlet.ServletRequestWrapper
(和javax.servlet.ServletResponseWrapper
, javax.servlet.http.HttpServletRequestWrapper
, javax.servlet.http.HttpServletResponseWrapper
)
Provides a convenient implementation of the
ServletRequest
interface that can be subclassed by developers wishing to adapt the request to a Servlet.提供
ServletRequest
接口的便捷实现,该接口可以由希望将请求调整为Servlet的开发人员进行子类化。 This class implements the Wrapper or Decorator pattern.此类实现Wrapper或Decorator模式。 Methods default to calling through to the wrapped request object.
方法默认调用包装的请求对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.