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. SQL Server 2012:SQL Server体系结构——一个查询的生命周期(第3部分)(完结)

    一个简单的更新查询 现在应该知道只读取数据的查询生命周期,下一步来认定当你需要更新数据时会发生什么.这个部分通过看一个简单的UPDATE查询,修改刚才例子里读取的数据,来回答. 庆幸的是,直到存取方法 ...

  2. JavaScript中的数组详解

    JavaScript中的数组 一.数组的定义 数组是值的有序集合,或者说数组都是数据的有序列表. 二.创建数组 [字面量形式] 1.空数组 var arr=[]; 2.带有元素的数组 var arr= ...

  3. sqlite3存储格式

    本篇介绍sqlite3数据库文件的存储格式.通过阅读源读源代码可以知道sqlite的设计思想.一个sqlite数据库文件对应着一个数据库.sqlite将数据库文件划分大小一致的存储(以区分内存)页面, ...

  4. Velocity魔法堂系列三:模板与宿主环境通信

    一.前言 Velocity作为历史悠久的模板引擎不单单可以替代JSP作为Java Web的服务端网页模板引擎,而且可以作为普通文本的模板引擎来增强服务端程序文本处理能力.而且Velocity被移植到不 ...

  5. UICollectionView使用以及与UITableView的区别

    在开始前我们在这先附一段最简单的代码 - (void)viewDidLoad { [super viewDidLoad]; UICollectionViewFlowLayout *layout = [ ...

  6. 第二sprint总结

    总结:在第二轮sprint中,团队的开发工作出现了极大的懈怠,团队成员分配的工作完成情况较差,进度较慢,希望能在最后一周中,完成预定的功能模块 个人贡献分: 124陈彦生:22=20*5*22% 14 ...

  7. 对Mathsapp的测试以及找bug

    组员博客地址: 练思明 卓嘉炜:http://www.cnblogs.com/luoliuxi/ 何宇明:http://www.cnblogs.com/40heyuming/ 团队贡献分: 练思明:2 ...

  8. VC包含目录、附加依赖项、库目录及具体设置

    包含目录:#include <headerfile.h>中headerfile.h的搜索目录.如果有XXX.h找不到,设置这个目录可以解决. 附加依赖项:C++的库会把函数.类的声明放在* ...

  9. ActiveReports 报表应用教程 (1)---Hello ActiveReports

    在开始专题内容之前,我们还是了解一下 ActiveReports 是一款什么产品:ActiveReports是一款在全球范围内应用非常广泛的报表控件,以提供.NET报表所需的全部报表设计功能领先于同类 ...

  10. jquery 全选 全不选 反选

    1.概述 在项目中经常遇到列表中对复选框进行勾选操作,全选...反选.. 2. example <html> <body> <form id="test-for ...