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. 程序代码记Log

    private static object s_lock = new object(); public static void TraceLog(string message, string logF ...

  2. SQL中对XML的处理

    DECLARE  @PreSOMasterXML XMLDECLARE   @SDA VARCHAR(100)SET @PreSOMasterXML=N'<ProcessTaskRequest& ...

  3. mysql rand()产生随机整数范围及方法

    根据官方文档,rand()的取值范围为[0,1) 若要在i ≤ R ≤ j 这个范围得到一个随机整数R ,需要用到表达式 FLOOR(i + RAND() * (j – i + 1))例如, 若要在7 ...

  4. 1215 spring 3 项目更新

    列志华 (组长) http://www.cnblogs.com/liezhihua/ 团队guihub https://github.com/LWHTF/OrderingFood 黄柏堂 http:/ ...

  5. java:[1,1] 需要class, interface或enum

    状态: cmd编译.java文件时报异常:java:[1,1] 需要class, interface或enum异常原因: 主要原因是java文件的编码问题. 在中文操作系统中,使用一贯的"j ...

  6. QTableView表格视图的列宽设置

    Qt中的表格控件可以通过从QTableView或QTableWidget派生子类实现.其中,QTableWidget只是对QTableView的一种简单封装.因为使用QTableView常常需要用户指 ...

  7. 用C#开发的双色球走势图(一)

    首先声明,个人纯粹无聊之作,不作商业用途. 我相信每个人都拥有一个梦想那就是有朝一日能中500W,这个也一直是我的梦想,并默默每一期双色球或多或少要贡献自己一点点力量,本人并不属于那种铁杆的彩票迷,每 ...

  8. ThinkPHP系的两个东东OneThink和ThinkCMF

    假设有这样一个命题:需要对一个已有系统进行移植,有没有什么系统是适合用来作为进行快速移植的基础的.能解决每个系统的基本问题,只需考虑相关业务逻辑问题. OneThink是TP团队官方出品. http: ...

  9. 泛函编程(10)-异常处理-Either

    上节我们介绍了新的数据类型Option:一个专门对付异常情况出现时可以有一致反应所使用的数据类型.Option可以使编程人员不必理会出现异常后应该如何处理结果,他只是获得了一个None值,但这个Non ...

  10. 【转】 StringUtils中 isNotEmpty 和isNotBlank的区别

    [转自]http://blog.csdn.net/foamflower/article/details/5713604 isNotEmpty将空格也作为参数,isNotBlank则排除空格参数 Str ...