zoukankan      html  css  js  c++  java
  • Effective Java 16 Favor composition over inheritance

    Inheritance disadvantage

    Unlike method invocation, inheritance violates encapsulation. Since you don't know the super class implementation which may involve some unpredictable calling between different methods of super class. And it is difficult to maintain the subclass when there is a change of the super class.

       

    Be careful with the interaction between inner methods of the superclass. Here is the demo.

       

    package com.effectivejava.classinterface;

       

    import java.util.Collection;

    import java.util.Set;

       

    /**

    * @author Kaibo

    *

    */

    // 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);

    }

       

    @Override

    public boolean addAll(Collection<? extends E> c) {

    addCount += c.size();

    return super.addAll(c);

    }

       

    public int getAddCount() {

    /* If you directly extends one implementation such as HashSet<E> of the Set<E> interface you will get the wrong addCount since the addAll method invoke the add method internally which has been override by your sub class.

    */

    return addCount;

    }

    }

       

    /**

    * Implementation of prefer to composite to inherence.

    */

    package com.effectivejava.classinterface;

       

    import java.util.Collection;

    import java.util.Iterator;

    import java.util.Set;

       

    /**

    * @author Kaibo

    *

    */

    // 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);

    }

       

    public boolean isEmpty() {

    return s.isEmpty();

    }

       

    public int size() {

    return s.size();

    }

       

    public Iterator<E> iterator() {

    return s.iterator();

    }

       

    public boolean add(E e) {

    return s.add(e);

    }

       

    public boolean remove(Object o) {

    return s.remove(o);

    }

       

    public boolean containsAll(Collection<?> c) {

    return s.containsAll(c);

    }

       

    public boolean addAll(Collection<? extends E> c) {

    return s.addAll(c);

    }

       

    public boolean removeAll(Collection<?> c) {

    return s.removeAll(c);

    }

       

    public boolean retainAll(Collection<?> c) {

    return s.retainAll(c);

    }

       

    public Object[] toArray() {

    return s.toArray();

    }

       

    public <T> T[] toArray(T[] a) {

    return s.toArray(a);

    }

       

    @Override

    public boolean equals(Object o) {

    return s.equals(o);

    }

       

    @Override

    public int hashCode() {

    return s.hashCode();

    }

       

    @Override

    public String toString() {

    return s.toString();

    }

    }

       

    Wrapper class (Composite) disadvantage

    Wrapper classes are not suited for use in callback frameworks, wherein objects pass self-references to other objects for subsequent invocations ("callbacks").

       

    To avoid the method invoke subclass's overrided version

    You can eliminate a class's self-use of overridable methods mechanically, without changing its behavior. Move the body of each overridable method to a private "helper method" and have each overridable method invoke its private helper method. Then replace each self-use of an overridable method with a direct invocation of the overridable method's private helper method.

    作者:小郝
    出处:http://www.cnblogs.com/haokaibo/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    用PHP写一个最简单的解释器Part4(写一个最简单的脚本语言)
    Java 引用传递
    Java 收集的代码 transient
    Java 继承 执行顺序
    Java 静态类 static
    Java 多态 虚方法
    Mongodb 安装
    入手Intel 750
    Intellij IDEA 创建控制台项目,断点调试快捷方式
    IntelliJ IDEA 的 Java 热部署插件 JRebel 安装及使用
  • 原文地址:https://www.cnblogs.com/haokaibo/p/favor-composition-over-inheritance.html
Copyright © 2011-2022 走看看