Principle When implement the singleton pattern please decorate the INSTANCE field with "static final" and decorate the constructor with "private". // Singleton with static factory public class Elvis { private static final Elvis INSTANC…
原文网址:http://iaiai.iteye.com/blog/1843553 1 背景 在java语言中还没有引入枚举类型之前,表示枚举类型的常用模式是声明一组具有int常量.之前我们通常利用public final static 方法定义的代码如下,分别用1 表示春天,2表示夏天,3表示秋天,4表示冬天. public class Season { public static final int SPRING = 1; public static final int SUMMER = …
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&q…
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 th…
1. Bean @Entity @Table(name = "entities") public class Entities { public enum entityType { location, group; } private entityType locationOrGroup; @Column(name="location_or_group") @Enumerated(EnumType.STRING) //Should set the type of l…
You can convert to an enum value from its underlying type by casting the underlying type (e.g. int) to the enum type. However, when you cast a value that doesn't have a corresponding enumerator in the type, you don't get any sort of error. In the exa…