Java高效编程(2) -- Creating and Destroying Objects
Item 1: Consider static factory methods instead of constructors
Advantage:
One advantage of static factory methods is that, unlike constructors, they have names.
A second advantage of static factory methods is that, unlike constructors, they are not required to create a new object each time they’re invoked.
A third advantage of static factory methods is that, unlike constructors, they can return an object of any subtype of their return type.
A fourth advantage of static factory methods is that they reduce the verbosity(冗长) of creating parameterized type instances.
Disadvantage:
The main disadvantage of providing only static factory methods is that classes without public or protected constructors cannot be subclassed.
A second disadvantage of static factory methods is that they are not readily distinguishable from other static methods.
Item 2: Consider a builder when faced with many constructor parameters
the telescoping constructor pattern works, but it is hard to write client code when there are many parameters, and harder still to read it.
a JavaBean may be in an inconsistent state partway through its construction.
the JavaBeans pattern precludes the possibility of making a class immutable.
Advantage:
The Builder pattern simulates named optional parameters.
Class.newInstance breaks compile-time exception checking.
the Builder pattern is a good choice when designing classes whose constructors or static factories would have more than a handful of parameters.
Item 3: Enforce the singleton property with a private constructor or an enum type
Making a class a singleton can make it difficult to test its clients.
a single-element enum type is the best way to implement a singleton.
#1. Singleton with public final field
public class Elvis {
public static final Elvis INSTANCE = new Elvis();
private Elvis() { ... }
public void leaveTheBuilding() { ... }
}
#2. Singleton with static factory
public class Elvis {
private static final Elvis INSTANCE = new Elvis();
private Elvis() { ... }
public static Elvis getInstance() { return INSTANCE; }
public void leaveTheBuilding() { ... }
}
#3. readResolve method to preserve singleton property
private Object readResolve() {
// Return the one true Elvis and let the garbage collector
// take care of the Elvis impersonator.
return INSTANCE;
}
#4. Enum singleton - the preferred approach
public enum Elvis {
INSTANCE;
public void leaveTheBuilding() { ... }
}
Item 4: Enforce noninstantiability with a private constructor
Attempting to enforce noninstantiability by making a class abstract does not work.
a class can be made noninstantiable by including a private constructor.
public class UtilityClass {
// Suppress default constructor for noninstantiability
private UtilityClass() {
throw new AssertionError();
}
... // Remainder omitted
}
Java高效编程(2) -- Creating and Destroying Objects的更多相关文章
- Java高效编程:总结分享
参考资料:慕课网:Java高效编程收费实战课程.博客园.CSDN.菜鸟教程以及其他文档. 篇幅受限,不太想针对每个点都写篇博客,有的地方可能写的不是很详细,一笔带过了.如果你觉得那个点在项目中用得上可 ...
- 剑指Java高效编程教程
教程介绍 所谓"武以快为尊,天下武功唯快不破".本课程剑指Java高效编程,致力于从"技术"和"工具"两大 维度提高编程效率,帮助广大程序员 ...
- Effective Java P2 Creating and Destroying Objects
This chapter concerns creating and destorying objects : when and how to create them, when and how to ...
- Creating and Destroying Objects
Consider static factpry methods instead of construction 四个优点两个缺点 One advantage of static factory met ...
- Java高效编程之二【对所有对象都通用的方法】
对于所有对象都通用的方法,即Object类的所有非final方法(equals.hashCode.toString.clone和finalize)都有明确的通用约定,都是为了要被改写(override ...
- Java 高效编程(Effective Java)中文第三版(补档)
来源:sjsdfg/effective-java-3rd-chinese <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过, ...
- Java高效编程之四【C语言结构的替代】
本章节可以跳过,但是[二十一]是非常有价值的 十九.用类代替结构 坚持以包含私有域和公有访问方法(accessor method)的类.Java平台中有几个类违反了“公有类不应该直接暴露数据域”的告诫 ...
- Java高效编程之三【类和接口】
本部分包含的一些指导原则,可以帮助哦我们更好滴利用这些语言元素,以便让设计出来的类更加有用.健壮和灵活. 十二.使类和成员的访问能力最小化 三个关键词访问修饰符:private(私有的=类级别的).未 ...
- Java高效编程之一【创建和销毁对象】
一.考虑用静态工厂方法替代构造函数 代表实现:java.util.Collection Framework Boolean类的简单例子: public static Boolean valueOf ( ...
随机推荐
- 《Linux Device Drivers》第十六章 块设备驱动程序——note
基本介绍 块设备驱动程序通过主传动固定大小数据的随机访问设备 Linux核心Visual块设备作为基本设备和不同的字符设备类型 Linux块设备驱动程序接口,使块设备最大限度地发挥其效用.一个问题 一 ...
- 了解HTML5和“她”的 API (三)
Web Workers(后台线程) JavaScript是单线程的,较长的javascript运算会阻塞UI线程. web worker 是运行在后台的 JavaScript,不会影响页面的性能. 在 ...
- cocos2d-x 3.1.1 学习笔记[17] 关于这些活动功能
供cocos2d-x通常使用的方法,我有一个好脸色.这项研究真的奖励. 向导首先,定义,实施一系列连续动作. 对于我们的行动能回调函数,我们必须申报并加以实施. void callBack(); vo ...
- C# 读取IE缓存文件(2)
private void button1_Click(object sender, EventArgs e) { , nBufSize; IntPtr buf; INTERNET_CACHE_ENTR ...
- [Cocos2d-x]在Cocos2d-x 3.x如何通过版本号WebSocket连接server数据的传输
WebSocket 首先新建一个空的目录,通过npm安装nodejs-websocket: npm install nodejs-websocket 新建app.js文件: var ws = requ ...
- Android UI法宝发展Angrytools
最近很多人问我,个人App开发商如何设计UI. 其实这是个人开发者最头疼,谁在搞技术,真的不能做的一切.不可能花大量的时间去切图,去做原型设计,去做美工. 当然,尽管我们设计不出那么复杂,静止的UI. ...
- Servlet实例解说
打开昨天上午,负责人突然问我,client控制信息,如何让在后台?我想回答:假设总体提交form,在C#使用代码request获取表单的内容.假设局部提交,在用JS和Ajax交互,通过Ajax的ope ...
- React-Native基础教程
React-Native牛刀小试仿京东砍啊砍砍到你手软 React-Native基础教程 *React-Native基础篇作者git *React-Native官方文档 *Demo 几个月前faceb ...
- Visual Studio 原生开发的10个调试技巧(二)
原文:Visual Studio 原生开发的10个调试技巧(二) 我以前关于 Visual Studio 调试技巧的文章引起了大家很大的兴趣,以至于我决定分享更多调试的知识.以下的列表中你可以看到写原 ...
- 跑Java -jar somefile.jar时会发生什么(一个)
最近阅读JVM源代码.一些想法写Blog分享.于是,他开了这么一个新课题. 第一篇文章取名字的时候让我很困惑,我代码的阅读是从Launcher開始入手的,也就是Java.exe(假设是windows平 ...