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的更多相关文章

  1. Java高效编程:总结分享

    参考资料:慕课网:Java高效编程收费实战课程.博客园.CSDN.菜鸟教程以及其他文档. 篇幅受限,不太想针对每个点都写篇博客,有的地方可能写的不是很详细,一笔带过了.如果你觉得那个点在项目中用得上可 ...

  2. 剑指Java高效编程教程

    教程介绍 所谓"武以快为尊,天下武功唯快不破".本课程剑指Java高效编程,致力于从"技术"和"工具"两大 维度提高编程效率,帮助广大程序员 ...

  3. Effective Java P2 Creating and Destroying Objects

    This chapter concerns creating and destorying objects : when and how to create them, when and how to ...

  4. Creating and Destroying Objects

    Consider static factpry methods instead of construction 四个优点两个缺点 One advantage of static factory met ...

  5. Java高效编程之二【对所有对象都通用的方法】

    对于所有对象都通用的方法,即Object类的所有非final方法(equals.hashCode.toString.clone和finalize)都有明确的通用约定,都是为了要被改写(override ...

  6. Java 高效编程(Effective Java)中文第三版(补档)

    来源:sjsdfg/effective-java-3rd-chinese <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过, ...

  7. Java高效编程之四【C语言结构的替代】

    本章节可以跳过,但是[二十一]是非常有价值的 十九.用类代替结构 坚持以包含私有域和公有访问方法(accessor method)的类.Java平台中有几个类违反了“公有类不应该直接暴露数据域”的告诫 ...

  8. Java高效编程之三【类和接口】

    本部分包含的一些指导原则,可以帮助哦我们更好滴利用这些语言元素,以便让设计出来的类更加有用.健壮和灵活. 十二.使类和成员的访问能力最小化 三个关键词访问修饰符:private(私有的=类级别的).未 ...

  9. Java高效编程之一【创建和销毁对象】

    一.考虑用静态工厂方法替代构造函数 代表实现:java.util.Collection Framework Boolean类的简单例子: public static Boolean valueOf ( ...

随机推荐

  1. Error Code: 1318. Incorrect number of arguments for PROCEDURE company.new_procedure; expected 2, got

    1.错误叙述性说明 20:27:34 call new_procedure(20150112) Error Code: 1318. Incorrect number of arguments for ...

  2. SQL Server 版本号汇总

    通过SSMS连接Sql servr,查看实例的版本就能知道当前SQL Server的版本号了.   RTM (no SP) SP1 SP2 SP3 SP4  SQL Server 2014     c ...

  3. LeetCodeOJ. Longest Common Prefix

    试题请參见: https://oj.leetcode.com/problems/longest-common-prefix/ 题目概述 Write a function to find the lon ...

  4. Android多线程.断点续传下载

    多线程,可断点续传的demo!最早写于2010.7! /** * @brief  主界面      * @author lixp  */ public class HomeActivity exten ...

  5. 行政歌节 &#183; 萧谱1

    4之前听 陈越 的<绿野仙踪> 版权声明:本文博客原创文章,博客,未经同意,不得转载.

  6. HDU 3126 Nova [2009 Asia Wuhan Regional Contest Online]

    标题效果 有着n巫妖.m精灵.k木.他们都有自己的位置坐标表示.冷却时间,树有覆盖范围. 假设某个巫妖攻击精灵的路线(他俩之间的连线)经过树的覆盖范围,表示精灵被树挡住巫妖攻击不到.求巫妖杀死所有精灵 ...

  7. UVA 439 Knight Moves(BFS)

    Knight Moves option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=3 ...

  8. MongoDB日常保养

    它引入了程序来进行维护管理工具 MongoDB的日常维护包含使用配置文件,设置訪问控制.Shell交互,系统监控和管理,数据库日常备份和恢复 启动和停止MongoDB 启动后能够通过数据库的IP加po ...

  9. Android中的应用——谷歌官方Json分析工具Gson使用

    一个.Gson基本介绍 Gson(又称Google Gson)是Google公司公布的一个开放源码的Java库.主要用途为串行化Java对象为JSON字符串,或反串行化JSON字符串成Java对象. ...

  10. 将odbc扩展编译至nodejs程序集中

    1. 下载nodejs https://github.com/joyent/node 2. 下载odbc for nodejs 扩展 https://registry.npmjs.org/odbc/- ...