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

    The easiest way to describe what Mono currently supports is:Everything in .NET 4.5 except WPF, WWF, ...

  2. go语言的安装和配置,以及包引用

    1.下载和安装 首先大家可以去官网下载 http://golang.org/dl/ 如果官网你看不懂的话,可以到这里下载: http://golangtc.com/download ,这里也提供了百度 ...

  3. 创建第一个freemarker

    1.创建java项目TestFreeMarker 2.导入包freemarker.jar,添加依赖 3.在根目录下创建一个文件夹templates 4.在文件夹templates下创建文件a.ftl ...

  4. 关于UIView 的autoresizingMask属性,即UIViewAutoresizing

    enum {   UIViewAutoresizingNone                 = 0,   UIViewAutoresizingFlexibleLeftMargin   = 1 &l ...

  5. android与javascript相互调用

    下面这一节来介绍android和javascript是怎么相互调用的,这样我们的UI界面设计起来就简单多了,而且UI设计起来也可以跨平台.现在有好多web app前台框架了,比如sencha和jque ...

  6. Sqlserver更新数据表xml类型字段内容某个节点值的脚本

    GO USE [JC2010_MAIN_DB] 1.新建备份表JobObjectVersion_JCSchemVersion_BCK) GO IF EXISTS (SELECT * FROM sys. ...

  7. 二十六个月Android学习工作总结

    1.客户端的功能逻辑不难,UI界面也不难,但写UI花的时间是写功能逻辑的两倍. 2.写代码前的思考过程非常重要,即使在简单的功能,也需要在本子上把该功能的运行过程写出来. 3.要有自己的知识库,可以是 ...

  8. Access to the path '....' is denied.解决方法

    昨天公司项目迁移服务器,从自己服务器迁移到阿里云服务器,部署完成后发现有一个页面要读取磁盘上的静态文件就报错了... 如图: 解决办法: 在 Web.Config 的 <System.Web&g ...

  9. prototype vs __proto__ 之间关系

    __proto__ is the actual object that is used in the lookup chain to resolve methods, etc. __proto__是解 ...

  10. HTML5 Canvas图片操作简单实例1

    1.加载显示图片 <canvas id="canvasOne" class="myCanvas" width="500" height ...