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 ( ...
随机推荐
- RH033读书笔记(7)-Lab 8 Introduction to String Processing
Lab 8 Introduction to String Processing Sequence 1: Exercises in string processing 1. Other than the ...
- 【Web探索之旅】第二部分第五课:响应式网站和移动应用
内容简介 1.第二部分第五课:响应式网站和移动应用 2.第三部分第一课预告:服务器 第二部分第五课:响应式网站和移动应用 在我们开始聊响应式网站之前,我们可以聊聊移动App(App是Applicati ...
- 【转】HLSL基础
原文地址http://blog.csdn.net/chpdirect1984/article/details/1911622 目录 前言 1.HLSL入门 1.1什么是着色器 1.2什么是HLSL 1 ...
- uva133 The Dole Queue ( 约瑟夫环的模拟)
题目链接: 啊哈哈,选我选我 思路是: 相当于模拟约瑟夫环,仅仅只是是从顺逆时针同一时候进行的,然后就是顺逆时针走能够编写一个函数,仅仅只是是走的方向的标志变量相反..还有就是为了(pos+flag+ ...
- EJB学习笔记
1 J2ee概述 J2ee是企业级的计算平台,它为分布式和基于组件的软件开发提供了一个“操作系统” Ant是什么工具?? EJB: 什么是EJB? 是一种server端组件结构,简化了开发分布式企 ...
- 【Linux探索之旅】开宗明义+第一部分第一课:什么是Linux?
内容简介 1.课程大纲 2.第一部分第一课:什么是Linux? 3.第一部分第二课预告:下载Linux,免费的噢! 开宗明义 我们总听到别人说:Linux挺复杂的,是给那些追求逼格的程序员用的.咱 ...
- 【C语言探索之旅】 第一部分第七课:循环语句
内容简介 1.课程大纲 2.第一部分第七课: 循环语句 3.第一部分第八课预告: 第一个C语言小游戏 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言编 ...
- css3 shadow为了实现各种漂亮的阴影效果
<!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/h ...
- 将cocos2dx+lua创建的游戏port到windows phone
在整个Port的过程中遇到的问题总结例如以下 1.一定要使用最新版本号的cocos2dx,原因大家看一下changelog就知道了,近期的cocos2dx版本号都是在修windows phone上的b ...
- transform:translateZ() 字体模糊问题 父类重返Z轴平面
translateZ()变糊 第一种情况: 当translateZ(m)中的 m设置为 非整数,1.5px 之类的,字体会模糊,但是不明显;和浏览器渲染,字体格式,或者操作系统有关, 这个 css中 ...