Principle

When implement the singleton pattern please decorate the INSTANCE field with "static final" and decorate the constructor with "private".

// 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() { ... }

}

NOTE

caveat: a privileged client can invoke the private constructor reflectively (Item 53) with the aid of the AccessibleObject.setAccessible method. If you need to defend against this attack, modify the constructor to make it throw an

exception if it's asked to create a second instance.

To handle the serializable object to be new object to the singleton object please implement the method below:

// 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;

}

If you use java release 1.5 or above you can just use enum type.

// Enum singleton - the preferred approach

public enum Elvis {

INSTANCE;

public void leaveTheBuilding() { ... }

}

Effective Java 03 Enforce the singleton property with a private constructor or an enum type的更多相关文章

  1. Effective Java Item3:Enforce the singleton property with a private constructor or an enum type

    Item3:Enforce the singleton property with a private constructor or an enum type 采用枚举类型(ENUM)实现单例模式. ...

  2. Java之创建对象>3.Enforce the singleton property with a private constructor or an enum type

     1. 通过一个公开的字段来获取单例 // Singleton with public final field public class Elvis { public static final Elv ...

  3. Effective Java Item4:Enforce noninstantiability with a private constructor

    Item4:Enforce noninstantiability with a private constructor 通过构造私有化,禁止对象被实例化. public class UtilClass ...

  4. Effective Java 04 Enforce noninstantiability with a private constructor

    A class can be made noninstantiable by including a private constructor. // Noninstantiable utility c ...

  5. Effective Java 02 Consider a builder when faced with many constructor parameters

    Advantage It simulates named optional parameters which is easily used to client API. Detect the inva ...

  6. Effective Java Item2:Consider a builder when faced with many constructor parameters

    Item2:Consider a builder when faced with many constructor parameters 当构造方法有多个参数时,可以考虑使用builder方式进行处理 ...

  7. 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 ...

  8. Effective Java 目录

    <Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...

  9. 【Effective Java】阅读

    Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...

随机推荐

  1. css居中学习笔记

    css居中学习笔记 一.水平居中 以下面的代码为例: <body> <div class="parent"> <div class="chi ...

  2. Device eth0 does not seem to be present,delaying initialization解决方法

    Bringing up interface eth0:  Device eth0 does not seem to be present, delaying initialization. 在linu ...

  3. Spring基础——一个简单的例子

    一.学习版本 spring-framework-4.0.0 二.导入 jar 包: 三.在类路径下创建 Spring Config 文件:ApplicationContext.xml <?xml ...

  4. Node.js爬虫抓取数据 -- HTML 实体编码处理办法

    cheerio DOM化并解析的时候 1.假如使用了 .text()方法,则一般不会有html实体编码的问题出现 2.如果使用了 .html()方法,则很多情况下(多数是非英文的时候)都会出现,这时, ...

  5. 后缀数组 --- HDU 3518 Boring counting

    Boring counting Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=3518 Mean: 给你一个字符串,求:至少出 ...

  6. math --- CSU 1554: SG Value

    SG Value Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1554 Mean: 一个可重集合,初始为空,每 ...

  7. mis导入器的加强版——vdproj文件资源浏览器

    上次做的那个导入器不够强大,限制还不小,用起来不方便.于是就再做一个这样的工具.代码基本上不同了,思想还是差不多,但功能肯定比之前的强大.经过了这次编写工具,对vdporj文件的了解又深一层了. 在v ...

  8. sql server索引功能资料

    无论何时对基础数据执行插入.更新或删除操作,SQL Server 数据库引擎都会自动维护索引.随着时间的推移,这些修改可能会导致索引中的信息分散在数据库中(含有碎片).当索引包含的页中的逻辑排序(基于 ...

  9. PHP的加密解密字符串函数

    程序中经常使用的PHP加密解密字符串函数 代码如下: /********************************************************************* 函数 ...

  10. ExtJs4 笔记(4) Ext.XTemplate 模板

    ExtJs4 笔记(4) Ext.XTemplate 模板 摘自:http://www.cnblogs.com/lipan/ 本篇将涉及到ExtJs中一个重要的概念,模板.话说Razor很神奇,但是我 ...