Effective Java 52 Refer to objects by their interfaces
Principle
- 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>();
- 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.
- 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的更多相关文章
- 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 ...
- 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 ...
- 《Effective Java》读书笔记 - 8.通用编程
Chapter 8 General Programming Item 45: Minimize the scope of local variables local variables应该在他们要被用 ...
- Effective Java 目录
<Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...
- Effective Java 第三版——10. 重写equals方法时遵守通用约定
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- 【Effective Java】阅读
Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...
- Effective Java 创建和销毁对象
<Effective Java>阅读笔记,用适合自己理解的方式提炼该书内容.<Effective Java>是一本很实用的书,阅读方法应该是快速的领会,总结,然后应用.而非,一 ...
- [Effective Java]第八章 通用程序设计
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- effective java读书小记(一)创建和销毁对象
序言 <effective java>可谓是java学习者心中的一本绝对不能不拜读的好书,她对于目标读者(有一点编程基础和开发经验)的人来说,由浅入深,言简意赅.每一章节都分为若干的条目, ...
随机推荐
- ASP.NETC#通用扩展函数之TypeParse 类型转换方便多了
用法: var int1 = "2".TryToInt();//转换为int失败返回0 var int2 = "2x".TryToInt(); var int3 ...
- struts2基础——标签
一.通用标签 1.s:property (读取值栈中对象的属性值) 属性:value:指定OGNL表达式:default:OGNL表达式返回为 null 时,使用默认值:escape:是否对 HTML ...
- servlet中的细节
Get方法有大小限制:1024个字符.这些信息使用 Query_String头传递,并通过Query_String环境变量访问.Post方法:请求体信息使用FromData头传递.读取所有表单参数:g ...
- sprint3冲刺团队贡献分-软件工程
蔡舜 : 20 卢晓洵 : 19 林宇粲 :22 王昕明 :21
- 三分套三分 --- HDU 3400 Line belt
Line belt Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3400 Mean: 给出两条平行的线段AB, CD,然后一 ...
- 新的Visual C++代码优化器
微软在 5 月 4 日发布了新的高级代码优化器,服务于 Visual C++ 的后端编译器.提高了代码性能,可以压缩代码体积,将编译器带入了一个新的境界. Visual C++ 的团队在博客上称,这将 ...
- c#重点[数据类型,构造方法,变量,变量,运算符,装箱,拆箱]
1.命名规范 类 :名词 每个单词的首字母大写 Dog Student PersonClass 字段:首个字母小写,如果有多个单词,后面的单词首字母大写 string name=" ...
- 译 PrestaShop开发者指南 第四篇 深入PrestaShop核心开发
## 访问数据库 ### 数据库结构 PrestaShop的数据库表默认带有ps_的前缀,前缀在安装时可以自定义. 所有表名都是小写,以下划线分割.当一个表表示要在两个实体间建立连接时,表名中两个实体 ...
- 邻接矩阵c源码(构造邻接矩阵,深度优先遍历,广度优先遍历,最小生成树prim,kruskal算法)
matrix.c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include < ...
- [moka同学收藏]Yii2.0 rules验证规则
required : 必须值验证属性 [['字段名'],required,'requiredValue'=>'必填值','message'=>'提示信息']; #说明:CRequiredV ...