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/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    P5047 [Ynoi2019 模拟赛] Yuno loves sqrt technology II
    P4887 【模板】莫队二次离线(第十四分块(前体))
    SP20644 ZQUERY
    企业战略管理【0612】
    管理经济学【0812】
    商务沟通[0664]
    国际企业管理【0813】
    社区管理【0272】
    战略管理【1059】
    管理沟通【1279】
  • 原文地址:https://www.cnblogs.com/haokaibo/p/prefer-interfaces-to-abstract-classes.html
Copyright © 2011-2022 走看看