有效 Java 第 16 项(第 2 版)- 转发 class 是否仅用于允许重复使用?
Effective Java item 16 (2nd edition) - Is Forwarding class only used to allow re-use?
我正在经历 Effective Java, Item-16 Favor composition over inheritance
。我查看了下面的 Forwarding class
示例。
我想知道 ForwardingSet
class 有什么意义? InstrumentedSet
可以很好地实现 Set
并拥有一个调用所有方法的私有实例。
如果我们最终有更多像 class 那样的 InstrumentedSet
将来只需要在基本行为之外做一些事情,它是否会促进重用并防止冗余?它只是面向未来的设计还是我还缺少其他东西?
// 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
是一个框架。
如果您必须编写多个 Set
在内部与其他 Set
一起工作但在 "vanilla" Set
之上提供不同的功能,您会公共部分最好写一次,不要多次。
Joshua Bloch,在 Effective Java 中将其称为 "composition",尽管实际实现看起来更像 decorator pattern。
Guava, as a class named ForwardingSet
.
中提供了实际的实现。
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?
是的。
Is it just future-proofing the design?
是的。
or is there something else to it that I am missing?
不,你没有遗漏任何东西。
是为了促进重用,防止冗余吗?是的。
它只是面向未来的设计吗?是的。
还有什么我想念的吗?没有。
"forwarding"通常被称为“委托”。
参见:What is the purpose of a delegation pattern?
Java 类 的一些示例,仅使用委托实现:
java.io.FilterInputStream
(and java.io.FilterOutputStream
, java.io.FilterReader
, java.io.FilterWriter
)
The class FilterInputStream
itself simply overrides all methods of InputStream
with versions that pass all requests to the contained input stream. Subclasses of FilterInputStream
may further override some of these methods and may also provide additional methods and fields.
javax.servlet.ServletRequestWrapper
(and 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. This class implements the Wrapper or Decorator pattern. Methods default to calling through to the wrapped request object.
我正在经历 Effective Java, Item-16 Favor composition over inheritance
。我查看了下面的 Forwarding class
示例。
我想知道 ForwardingSet
class 有什么意义? InstrumentedSet
可以很好地实现 Set
并拥有一个调用所有方法的私有实例。
如果我们最终有更多像 class 那样的 InstrumentedSet
将来只需要在基本行为之外做一些事情,它是否会促进重用并防止冗余?它只是面向未来的设计还是我还缺少其他东西?
// 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
是一个框架。
如果您必须编写多个 Set
在内部与其他 Set
一起工作但在 "vanilla" Set
之上提供不同的功能,您会公共部分最好写一次,不要多次。
Joshua Bloch,在 Effective Java 中将其称为 "composition",尽管实际实现看起来更像 decorator pattern。
Guava, as a class named ForwardingSet
.
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?
是的。
Is it just future-proofing the design?
是的。
or is there something else to it that I am missing?
不,你没有遗漏任何东西。
是为了促进重用,防止冗余吗?是的。
它只是面向未来的设计吗?是的。
还有什么我想念的吗?没有。
"forwarding"通常被称为“委托”。
参见:What is the purpose of a delegation pattern?
Java 类 的一些示例,仅使用委托实现:
java.io.FilterInputStream
(andjava.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. Subclasses ofFilterInputStream
may further override some of these methods and may also provide additional methods and fields.javax.servlet.ServletRequestWrapper
(andjavax.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. This class implements the Wrapper or Decorator pattern. Methods default to calling through to the wrapped request object.