zoukankan      html  css  js  c++  java
  • Effective Java 18 Prefer interfaces to abstract classes

    Feature

    Interface

    Abstract class

    Defining a type that permits multiple implementations

    Y

    Y

    Permitted to contain implementations.

    N

    Y

    The implemented class must reside the class hierarchy.

    N

    Y

    Single inheritance

    N

    Y

    Easy to evolve

    N

    Y

       

    Advantages of Interfaces

    1. Existing classes can be easily retrofitted to implement a new interface.
    2. Interfaces are ideal for defining mixins.
    3. Interfaces allow the construction of nonhierarchical type frameworks. (Composite)

      public interface Singer {

      AudioClip sing(Song s);

      }

      public interface Songwriter {

      Song compose(boolean hit);

      }

      public interface SingerSongwriter extends Singer, Songwriter {

      AudioClip strum();

      void actSensitive();

      }

    4. Interfaces enable safe, powerful functionality enhancements .
    You can combine the virtues of interfaces and abstract classes by providing an abstract skeletal implementation class to go with each nontrivial interface that you export.

    // Concrete implementation built atop skeletal implementation

    static List<Integer> intArrayAsList(final int[] a) {

    if (a == null)

    throw new NullPointerException();

    return new AbstractList<Integer>() {

    public Integer get(int i) {

    return a[i]; // Autoboxing (Item 05)

    }

    @Override public Integer set(int i, Integer val) {

    int oldVal = a[i];

    a[i] = val; // Auto-unboxing

    return oldVal; // Autoboxing

    }

    public int size() {

    return a.length;

    }

    };

    }

    Simulated multiple inheritance

    The class implementing the interface can forward invocations of interface methods to a contained instance of a private inner class that extends the skeletal implementation.

    // Skeletal Implementation

    public abstract class AbstractMapEntry<K, V> implements Map.Entry<K, V> {

    // Primitive operations

    public abstract K getKey();

    public abstract V getValue();

    // Entries in modifiable maps must override this method

    public V setValue(V value) {

    throw new UnsupportedOperationException();

    }

    // Implements the general contract of Map.Entry.equals

    @Override

    public boolean equals(Object o) {

    if (o == this)

    return true;

    if (!(o instanceof Map.Entry))

    return false;

    Map.Entry<?, ?> arg = (Entry<?, ?>) o;

    return equals(getKey(), arg.getKey())

    && equals(getValue(), arg.getValue());

    }

    private static boolean equals(Object o1, Object o2) {

    return o1 == null ? o2 == null : o1.equals(o2);

    }

    // Implements the general contract of Map.Entry.hashCode

    @Override

    public int hashCode() {

    return hashCode(getKey()) ^ hashCode(getValue());

    }

    private static int hashCode(Object obj) {

    return obj == null ? 0 : obj.hashCode();

    }

    }

    Note

    1. Because skeletal implementationsare designed for inheritance, you should follow all of the design and documentation guidelines in Item 17.
    2. A minor variant on the skeletal implementation is the simple implementation. It differs by being not abstract. It's just simplest possible working implementation. You can use it as it stands or subclass it as circumstances warrant.

    Summary

    If you export a nontrivial interface, you should strongly consider providing a skeletal implementation to go with it. Once an interface is released and widely implemented, it is almost impossible to change. The best thing to do when releasing a new interface is to have as many programmers as possible implement the interface in as many ways as possible before the interface is frozen. Finally, you should design all of your public interfaces with the utmost care and test them thoroughly by writing multiple implementations

     

    作者:小郝
    出处:http://www.cnblogs.com/haokaibo/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    [再寄小读者之数学篇](2015-06-24 积分不等式)
    揭秘:三国时能令诸葛亮自叹不如的奇才是谁?
    【大话三国】揭秘蜀汉五虎将的真相
    三国揭秘 诸葛亮为何重用张飞疏远关羽
    [再寄小读者之数学篇](2015-06-08 一个有意思的定积分计算)
    咏史---左思
    非洲雄狮捕猎未遂被野牛群追赶逃到树上
    诚信,聪明,快乐,地位与竞争
    [裴礼文数学分析中的典型问题与方法习题参考解答]4.3.26
    [裴礼文数学分析中的典型问题与方法习题参考解答]4.3.11
  • 原文地址:https://www.cnblogs.com/haokaibo/p/prefer-interfaces-to-abstract-classes.html
Copyright © 2011-2022 走看看