简体   繁体   English

有效的Java项目16(第2版) - Forwarding类是否仅用于允许重用?

[英]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);
       }
       ...
    }

Yes, 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类的一些示例:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM