1. 通过一个公开的字段来获取单例

// Singleton with public final field
public class Elvis {
public static final Elvis INSTANCE = new Elvis();
private Elvis() { ... }
public void leaveTheBuilding() { ... }
}

The main advantage of the public field approach is that the declarations make it clear that the class is a singleton: the public static field is final, so it will always contain the same object reference. There is no longer any performance advantage to the public field approach (没有任何性能优势)

2. 通过一个公开的方法getInstance()来获取单例


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

One advantage of the factory-method approach is that it gives you the flexibility to change your mind about whether the class should be a singleton without changing its API. The factory method returns the sole instance but could easily be modified to return, say, a unique instance for each thread that invokes it. A second advantage, concerning generic types. Often neither of these advantages is relevant, and the final-field approach is simpler.

3.通过枚举来实现

// Enum singleton - the preferred approach
public enum Elvis {
INSTANCE;
public void leaveTheBuilding() { ... }
}

While this approach has yet to be widely adopted, a single-element enum type is the best way to implement a singleton.

Java之创建对象>3.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. 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 ...

  3. Java之创建对象>4.Enforce noninstantiability with a private constructor

    如果你定义的类仅仅是包含了一些静态的方法和静态的字段,这些类一般是一些工具类,这些一般是设计为不能被实例化的. 1. Attempting to enforce noninstantiability ...

  4. Java中创建对象的几种方式

    Java中创建对象的五种方式: 作为java开发者,我们每天创建很多对象,但是我们通常使用依赖注入的方式管理系统,比如:Spring去创建对象,然而这里有很多创建对象的方法:使用New关键字.使用Cl ...

  5. java.lang.IllegalStateException:Web app root system property already set to different value 错误原因及解决 Log4j

    Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件.甚至是套接口 服务器.NT的事件记录器.UNIX Syslog守护进程等: ...

  6. java中需要关注的3大方面内容/Java中创建对象的几种方法:

    1)垃圾回收 2)内存管理 3)性能优化 Java中创建对象的几种方法: 1)使用new关键字,创建相应的对象 2)通过Class下面的new Instance创建相应的对象 3)使用I/O流读取相应 ...

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

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

  8. Java中创建对象的五种方式

    我们总是讨论没有对象就去new一个对象,创建对象的方式在我这里变成了根深蒂固的new方式创建,但是其实创建对象的方式还是有很多种的,不单单有new方式创建对象,还有使用反射机制创建对象,使用clone ...

  9. 【转】Java中创建对象的5种方式

    Java中创建对象的5种方式   作为Java开发者,我们每天创建很多对象,但我们通常使用依赖管理系统,比如Spring去创建对象.然而这里有很多创建对象的方法,我们会在这篇文章中学到. Java中有 ...

随机推荐

  1. 开源项目MultiChoiceAdapter详解(五)——可扩展的MultiChoiceBaseAdapter

    上次写到了开源项目MultiChoiceAdapter详解(四)——MultiChoiceBaseAdapter的使用,其实我们仍旧可以不使用ActionMode的,所以这里就写一个自己扩展的方法. ...

  2. Visual Studio 2013 智能提示功能消失解决办法

    Visual Studio 2013中,智能提示功能突然用不了,查了一下,使用命令行重置VS的方法解决了这个问题.步骤如下: 开始菜单 -->所有程序-->Visual Studio 20 ...

  3. android R.layout 中找不到已存在的布局文件

    在R.layout.test文件时,总是找不到您想要的文件,可是它明明就在layout文件下面,而且在R.Java中也已经生成了,那么找不到的原因就是你导入了Android.R的包,这样你永远找不到你 ...

  4. [转]在ubuntu linux下以编译方式安装LAMP(apache mysql php)环境

    FROM : http://www.cnblogs.com/eleganthqy/archive/2010/02/28/1675217.html 最近转向到了使用ubuntu做桌面,安装好系统以来一直 ...

  5. 【UOJ Easy Round #1】

    数论/Trie/并查集 猜数 这题我是这样分析的…… $a*b=g*l=n=k^2 \ and \ (g|a,g|b) \Rightarrow (g*a')*(g*b' )=g*l=k^2 \\ \R ...

  6. 一致性哈希算法(consistent hashing)(转)

    原文链接:每天进步一点点——五分钟理解一致性哈希算法(consistent hashing)  一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法,设计目标是为了解决因特网 ...

  7. dcm4chee 修改默认(0002,0013) ImplementationVersionName

    dcm4chee-2.17.3-psql\server\default\lib\dcm4che.jar ----org\dcm4che\Implementation.properties dcm4ch ...

  8. 一个简易的WebServer程序

    现在这个公司的it管理真不知道说什么了,我主要从事.net方向的开发,公司电脑配置还是win7了,可是自己没有安装iis的权限,it也拒绝安装,想玩玩web安全都不行.于是乎决定开发一个window ...

  9. sql server 由于登入失败而无法启动服务

    到控制面板——管理工具——服务,找到mssqlserver这个服务,在属性里把登陆帐户改成你目前登录windows的帐户或选择本地系统账户再重新启动服务就好了

  10. 【ElasticSearch】ElasticSearch-索引优化-自定义索引

    ElasticSearch-索引优化-自定义索引 es 指定 索引 字段_百度搜索 [es]创建索引和映射 - 匡子语 - 博客园 reindex,增加字段,并新增数据 - Elastic中文社区 e ...