Item3:Enforce the singleton property with a private constructor or an enum type

采用枚举类型(ENUM)实现单例模式。

public enum Elvis {
INSTANCE; public void leaveTheBuiding(){
System.out.println("a single-element enum type is the best way to implement a singleton");
}
}

使用方式:

Elvis.INSTANCE.leaveTheBuiding();

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

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

    Principle When implement the singleton pattern please decorate the INSTANCE field with "static ...

  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. Qt入门(5)——用Qt控件创建一个电话本界面

    具体实现步骤: 一.首先用 Qt Designer 创建一个两张图的对话框,分别保存为listdialog.ui和editdialog.ui文件 要注意其中各个空间对应的名称修改好 二.新建一个Qt应 ...

  2. [置顶] 如何运行用记事本写的java程序

               今天用记事本写了一个java程序,测试能运行,现在把它分解成几个步骤,利于大家理解:           1. 新建一个记事本,后缀名是  .java  :然后在里面写一段jav ...

  3. ASP.NET MVC4.0 部署

    EntifyFramework 5.0.0 安装 http://www.nuget.org/packages/EntityFramework/5.0.0 1. 文章,部署前的配置 http://www ...

  4. zookeeper[6] zookeeper FAQ(转)

    转自:http://www.cnblogs.com/zhengran/p/4601855.html 1. 如何处理CONNECTION_LOSS?在Zookeeper中,服务器和客户端之间维持一个长连 ...

  5. java socket报文通信(一) socket的建立

    TCP是Transfer Control Protocol的 简称,是一种面向连接的保证可靠传输的协议.通过TCP协议传输,得到的是一个顺序的无差错的数据流.发送方和接收方的成对的两个socket之间 ...

  6. Http请求的 HttpURLConnection 和 HttpClient

    HTTP 请求方式: GET和POST的比较 请求包.png 例子.png 响应包.png 例子.png 请求头描述了客户端向服务器发送请求时使用的http协议类型,所使用的编码,以及发送内容的长度, ...

  7. fstab的格式

    # /etc/fstab/dev/hda8 swap swap defaults 0 0/dev/hda9 / ext2 defaults 1 1/dev/hda6 /wine vfat defaul ...

  8. C#。3.1 循环(叠加、穷举)

    循环. for 循环 嵌套的应用, 迭代.穷举 一.迭代法 每次循环都是从上次运算结果中获得数据,本次运算的结果都是要为下次运算做准备.例:1.100以内所有数的和. int sum = 0; for ...

  9. hibernate连接数据库,进行操作的步骤

    //初始化 Configuration conf=null; SessionFactory sf=null; Session session=null; Transaction tx=null; tr ...

  10. hibernate 核心总结 (面试)

    1:1(类与类之间) husband----wife 外键关联: a)单向@OneToOne b)双向@OneToOne, mappedby="husband" --------- ...