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学习者心中的一本绝对不能不拜读的好书,她对于目标读者(有一点编程基础和开发经验)的人来说,由浅入深,言简意赅.每一章节都分为若干的条目, ...
随机推荐
- 设计模式之Iterator模式(2)
这篇文章比较简单,作一个笔记. 模拟Iterator. Iterator接口: package cn.asto.Interator; public interface Iterator { publi ...
- sitemesh学习笔记(1)
最近在学习web开发的时候,发现很多的页面都存在同样的导航栏,登陆栏,js,jQuery等等相同的元素.这样就感觉开发变得好臃肿啊,并且,有时候改一个元素,就要把所有包含这个元素的页面全部重新码一遍, ...
- 说说jsonp
什么是jsonp jsonp充其量只能说是一种"方法".它可以让页面从其他域中获取资料. 首先要知道的是同源策略,在javascript中使用http请求(ajax)是会受到同 ...
- mysql update中需要根据条件列更新写法update case
以下两条语句是否可以合并成一条: where b>'2015-10-12'; , e='2015-01-01' where b='2015-10-12'; 既然来写博客了,那答案肯定是可以的, ...
- Scrum项目5.0
1.团队成员完成自己认领的任务. 2.燃尽图:理解.设计并画出本次Sprint的燃尽图的理想线.参考图6. 3.每日立会更新任务板上任务完成情况.燃尽图的实际线,分析项目进度是否在正轨. 每天的 ...
- Symantec Backup Exec恢复数据库
Insus.NET是使用Symantec Backup Exec来备份数据以及一些服务器文件.下面步骤是怎样恢复一个数据库.当我们数据库有问题,或是想恢复某一天的数据,得需要操作数据恢复Restore ...
- 消灭Bug!18款最佳的问题跟踪管理应用程序
摘要:工欲善其事,必先利其器,对于开发者来说,处理Bug是一件比较头疼的事,那么如何高效地解决Bug,选择一款合适的Bug跟踪处理工具会让你事半功倍. 对于开发者来说,Bug往往是他们最头疼的问题.有 ...
- Orchard 异常
在Orchard项目中碰的各种错误整理 1 打开网站出现404 . 查看日志 日志位置Orchard.Web\App_Data\Logs\orchard-error-2016.03.23.log 错误 ...
- ahjesus ubuntu10.4安装ruby2.1.1
sudo apt-get install python-software-properties sudo apt-add-repository ppa:brightbox/ruby-ng sudo a ...
- Python迭代器:捕获Generator的返回值
但是用for循环调用generator时,发现拿不到generator的return语句的返回值.如果想要拿到返回值,必须捕获StopIteration错误,返回值包含在StopIteration的v ...