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.

Effective Java 16 Favor composition over inheritance的更多相关文章

  1. Effective Java 26 Favor generic types

    Use generic types to replace the object declaration Add one or more type parameters to its declarati ...

  2. Effective Java 27 Favor generic methods

    Static utility methods are particularly good candidates for generification. The type parameter list, ...

  3. Effective Java 60 Favor the use of standard exceptions

    Benefits to reuse preexisting exceptions It makes your API easier to learn and use. Programs using y ...

  4. Effective Java Index

    Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...

  5. 《Effective Java》读书笔记 - 4.类和接口

    Chapter 4 Classes and Interfaces Item 13: Minimize the accessibility of classes and members 一个好的模块设计 ...

  6. Effective Java Chapter4 Classes and Interface

    MInimize the accessibility of classes and members 这个叫做所谓的 information hiding ,这么做在于让程序耦合度更低,增加程序的健壮性 ...

  7. Effective Java 目录

    <Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...

  8. 【Effective Java】阅读

    Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...

  9. 《Effective Java》读书笔记 - 3.对于所有对象都通用的方法

    Chapter 3 Methods Common to All Objects Item 8: Obey the general contract when overriding equals 以下几 ...

随机推荐

  1. 十分钟搞懂什么是CGI

    原文:CGI Made Really Easy,在翻译的过程中,我增加了一些我在学习过程中找到的更合适的资料,和自己的一些理解.不能算是严格的翻译文章,应该算是我的看这篇文章的过程的随笔吧. CGI真 ...

  2. intellij 调试方法

    intellij 调试方法 转自 http://www.th7.cn/Program/net/201410/296492.shtml

  3. Android 学习笔记之AndBase框架学习(六) PullToRefrech 下拉刷新的实现

    PS:Struggle for a better future 学习内容: 1.PullToRefrech下拉刷新的实现...   不得不说AndBase这个开源框架确实是非常的强大..把大部分的东西 ...

  4. Linux内核知识

    版本 linus树 Linux内核最初创始人--Linus Torvalds管理一个Linus树,linus树也称为主线(mainline).一般指的upstream,"上游",也 ...

  5. 关于开发Windows服务程序容易搞混的地方!

    在开发Windows服务程序时,我们一般需要添加安装程序,即:serviceInstaller,里面有几个关于名称属性,你都搞明白了吗? 1.Description:表示服务说明(描述服务是干什么的) ...

  6. .NET中的枚举(Enum)

    摘要:.NET中的枚举分为简单枚举和标志枚举,这次主要总结一下标志枚举适用条件,以及它的使用方法,并在文章的最后列举枚举使用的一些规范. 在刚接触.NET的枚举时,只用简单的枚举,对于标记枚举,只知道 ...

  7. shell join 参数详细说明

    join类似db里面的join方法,同样有left join right join inner join等 指定参数-a 可以指定join的方式. -a1表示显示第一个文件中不匹配的行,即为left ...

  8. Spring MVC 配置文件dispatcher-servlet.xml 文件详解

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  9. 重构第7天 重命名(Rename )

    理解:重命名就是把一些函数.字段.类.参数的名称 重命名为易于理解,最好是和自身的意义相同的名称.这样更易于理解,也可以减少大量的注释,名字即含义. 详解: 这个重构方法是我经常也是最常用的一种.我们 ...

  10. Requested registry access is not allowed(不允许所请求的注册表访问权)

    尝试创建自定义事件日志时,将会收到“Requested registry access is not allowed(不允许所请求的注册表访问权)”错误消息 EventLog.CreateEventS ...