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/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    android不知不觉偷拍他人功能实现(手机关闭依然拍照)【申明:来源于网络】
    地图标绘系统V1.0测试版【申明:来源于网络】
    AngularJS资源合集[备忘]【申明:来源于网络】
    java+js实现展示本地文件夹下的所有图片demo[申明:来源于网络]
    angular源码分析 摘抄 王大鹏 博客 directive指令及系列
    jQuery学习之prop和attr的区别
    顽Shi的实践总结
    URL 中#号,? ,&的作用 (摘抄整理 链接为学习地址)
    YeoMan 与Angularjs
    an'gularjs 环境搭建之NodeJS、NPM安装配置步骤(windows版本)
  • 原文地址:https://www.cnblogs.com/haokaibo/p/favor-composition-over-inheritance.html
Copyright © 2011-2022 走看看