Principle

  1. If appropriate interface types exist, then parameters, return values, variables, and fields should all be declared using interface types. The only time you really need to refer to an object's class is when you're creating it with a constructor.

    // Good - uses interface as type

    List<Subscriber> subscribers = new Vector<Subscriber>();

  2. If you depend on any special properties of an implementation, document these requirements where you declare the variable.

    One caveat

    If the original implementation offered some special functionality not required by the general contract of the interface and the code depended on that functionality, then it is critical that the new implementation provide the same functionality.

  3. It is entirely appropriate to refer to an object by a class rather than an interface if no appropriate interface exists.

    Examples:

  • Value classes - String, BigDecimal
  • Class-based framework - java.util.TimerTask
  • Classes that implement an interface but provide extra methods not found in the interface - LinkedHashMap

    Summary

    In practice, it depends whether a given object has an appropriate interface. If it does, your program will be more flexible if you use the interface to refer to the object; if not, just use the least specific class in the class hierarchy that provides the required functionality.

Effective Java 52 Refer to objects by their interfaces的更多相关文章

  1. Effective Java 21 Use function objects to represent strategies

    Theory In the Java the function pointers is implemented by the declaring an interface to represent s ...

  2. 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 ...

  3. 《Effective Java》读书笔记 - 8.通用编程

    Chapter 8 General Programming Item 45: Minimize the scope of local variables local variables应该在他们要被用 ...

  4. Effective Java 目录

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

  5. Effective Java 第三版——10. 重写equals方法时遵守通用约定

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  6. 【Effective Java】阅读

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

  7. Effective Java 创建和销毁对象

    <Effective Java>阅读笔记,用适合自己理解的方式提炼该书内容.<Effective Java>是一本很实用的书,阅读方法应该是快速的领会,总结,然后应用.而非,一 ...

  8. [Effective Java]第八章 通用程序设计

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  9. effective java读书小记(一)创建和销毁对象

    序言 <effective java>可谓是java学习者心中的一本绝对不能不拜读的好书,她对于目标读者(有一点编程基础和开发经验)的人来说,由浅入深,言简意赅.每一章节都分为若干的条目, ...

随机推荐

  1. "浅谈Android"第二篇:活动(Activity)

        距离上一篇文章,过去有半个多月了,在此期间忙于工作,疏于整理和总结,特此写下这篇博文,来谈谈自己对Activity的理解.总所周知,Activity组件在Android中的重要性不言而喻,我们 ...

  2. C#函数式程序设计之代码即数据

    自3.5版本以来,.NET以及微软的.NET语言开始支持表达式树.它们为这些语言的某个特定子集提供了eval形式的求值功能.考虑下面这个简单的Lambda表达式: Func<int, int, ...

  3. [Solution] NPOI操作Excel

    NPOI 是 POI 项目的 .NET 版本.POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目.使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 ...

  4. Sprint第三个冲刺(第七天)

    项目基本上可以说完成了,只是还有些小bug要修复.

  5. Symantec Backup Exec恢复数据库

    Insus.NET是使用Symantec Backup Exec来备份数据以及一些服务器文件.下面步骤是怎样恢复一个数据库.当我们数据库有问题,或是想恢复某一天的数据,得需要操作数据恢复Restore ...

  6. 常用vs快捷键

    Ctrl+E,D ----格式化全部代码 Ctrl+A+K+FCtrl+E,F ----格式化选中的代码 Ctrl+K+FCTRL + SHIFT + B生成解决方案 Alt+B+B 或 F6 生成当 ...

  7. csharp: WebBrowser read baidumap

    setpoint.html: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Typ ...

  8. csharp:引入app.manifest,程序在win7下以管理员权限运行配置方法

    https://msdn.microsoft.com/en-us/library/windows/desktop/hh848036(v=vs.85).aspx https://msdn.microso ...

  9. oracle client ORA-12541: TNS: 无监听程序

    1. Question description: if you are setting the oracle client to add a local network service,  you m ...

  10. 【JVM】2、关于jdk7的MethodHandle类

    关于MethodHandle类,这个类是在jdk1.7之后加入的,这个类的作用类似函数指针的意思 这个类中有一个方法 这里我的jdk有一个问题,就是我在进行MethodHandle操作的时候,我们会发 ...