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. UML建模之时序图

    现在是二月,而且到如今你或许已经读到.或听到人们谈论UML 2.0 —— 包括若干进步的 UML 的新规范,所做的变化.考虑到新规范的重要性,我们也正在修改这个文章系列的基础,把我们的注意力从 OMG ...

  2. android实现类似于支付宝余额快速闪动的效果

    效果如下:   此图片不会动,但实际上是会快速跳动的.  之前看到有支付宝的效果非常牛逼.就是进去看到余额呼噜噜的直接上蹿下跳到具体数字,效果帅,但不知道怎么实现,最近终于知道了. 思路: 首先经常用 ...

  3. NERDTree这个插件的用法简介

    事情是这样子的,想做做李治军老师班的操作系统实验,但是Linux上的gedit太简陋了(这个简陋程度堪比Windows环境下的"记事本"),被杨世祺大神嘲笑了.我心想既然在linu ...

  4. 超酷创意HTML5动画演示及代码

    HTML5是未来的网页开发神器,今天分享的这些HTML5动画大部分利用了CSS3的动画属性来实现,废话不多说,直接上演示和代码. HTML5/CSS3实现大风车旋转动画 这次我们要来分享一款很酷的HT ...

  5. cocos2d-x多分辨率适配原理分析(2.0.4之后的版本)

    2013年11月4日补充: 之前写这篇博客的时候其实我还没有开始过真正的去做一个项目,主要过程还是偏向于理解原理.前几天在准备练练手时回过头来想了下这个问题,发现又有点一头雾水了,所以我觉得之前我并没 ...

  6. Geeks Interview Question: Ugly Numbers

    Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence1, 2, 3, 4, 5, 6, 8, 9, ...

  7. xml to json

    // Changes XML to JSONfunction xmlToJson(xml) {    // Create the return object    var obj = {};    i ...

  8. kvm cobbler无人值守批量安装操作系统

    kvm cobbler无人值守批量安装操作系统 cobbler:一个自动网络安装系统的工具,集成PEX.dhcp.dns.tftpd.sync等服务.可以供大家管理安装操作系统 kvm:Linux系统 ...

  9. 设计模式16---设计模式之组合模式(Composite)(行为型)

    1.场景模拟 使用软件模拟大树的根节点和树枝节点和叶子节点 抽象为两类,容器节点和叶子节点 2.不用模式的解决方案 package demo14.composite.example1; import ...

  10. mybati之运行过程

    mybatis其实就只有两个配置文件(mybatis-config.xml与mapper.xml) mybatis-config.xml配置基本的数据,和数据源,全局参数 mapper.xml 多个s ...