enum singleton
来源:互联网 发布:双肩电脑包 知乎 编辑:程序博客网 时间:2024/04/30 13:46
原文:http://en.wikipedia.org/wiki/Singleton_pattern#The_Enum-way
In the second edition of his book Effective Java, Joshua Bloch claims that "a single-element enum type is the best way to implement a singleton" for any Java that supports enums. The use of an enum is very easy to implement and has no drawbacks regarding serializable objects, which have to be circumvented in the other ways.
public enum Singleton { INSTANCE; public void execute (String arg) { //... perform operation here ... }}
The public method can be written to take any desired types of arguments; a single String argument is used here as an example.
This approach implements the singleton by taking advantage of Java's guarantee that any enum value is instantiated only once in a Java program. Since Java enum values are globally accessible, so is the singleton. The drawback is that the enum type is somewhat inflexible; for example, it does not allow lazy initialization.
- enum singleton
- Java Enum Singleton
- enum方式实现singleton对象
- java用enum实现singleton的方法
- Why Enum Singleton are better in Java
- 枚举单例(Enum Singleton)
- Java - Why Enum Singleton are better in Java
- Singleton in action with enum- the preferred approach
- enum
- enum
- enum
- enum
- enum
- enum
- enum
- ENUM
- enum
- enum
- 删除信息前提示
- (4)设计模式方法-创建型设计模式--:简单工厂模式【【【工厂方法模式
- 怎么才能不学习、不工作。(懒人必看)
- 找工作
- java 构造函数是如何执行的
- enum singleton
- hdu1867之KMP
- [zz] main函数之前--谁动了我的main
- 以一个具体的例子,深入剖析一下Android系统的绑定机制
- NYOJ 14-会场安排问题
- cygwin的安装中途种种遭遇
- void cdev_init(struct cdev *cdev, const struct file_operations *fops)
- 小米即将完成新一轮融资 传估值近百亿美元
- ubuntu ssh