多元素枚举还是单例?
Multiple-Element Enum still Singleton?
众所周知,实现单例的最佳方法之一是使用 Enum
,因 Josh Bloch 而流行。
他说:
This approach is functionally equivalent to the public field approach, except that it is more concise, provides the serialization machinery for free, and provides an ironclad guarantee against multiple instantiation, even in the face of sophisticated serialization or reflection attacks. While this approach has yet to be widely adopted, a single-element enum type is the best way to implement a singleton.
这是否仍然适用于多元素 Enum
?每个元素不会只被实例化一次吗?
感知到的好处是有几个相似但不同的单例枚举,例如:每个元素注入不同的配置。
示例:
public enum Beatles {
george("George"),
john("John"),
paul("Paul"),
ringo("Ringo"),
;
private Beatles(final String name) {
this.name = name;
}
private final String name;
public String getName() {
return name;
}
}
您可以使用标记接口扩展单元素枚举范例以支持 "similar but different" 单例:
public interface MySingletonInterface {
void doFoo(Bar bar);
}
那么你的枚举可以实现接口:
public enum FirstSingletonEnum implements MySingleTonInterface {
...
@Override
public void doFoo(Bar bar) {
... //do something with bar
}
}
public enum SecondSingletonEnum implements MySingleTonInterface {
...
@Override
public void doFoo(Bar bar) {
... //do something slightly different with Bar
}
}
我觉得这是一种更简洁的方法,它将所有实现都塞进了一个枚举中。
就枚举内部的逻辑而言,我会非常小心地处理这个问题。总的来说,我认为枚举可以封装逻辑,只要该逻辑与每个枚举实例所代表的内容以及枚举作为一个整体所代表的内容紧密耦合即可。如果您正在执行诸如引入其他服务或与数据库等外部实体通信之类的操作,那么枚举可能不是正确的选择。
enum A {INSTANCE};
内部等同于
class A {
public static final A INSTANCE = new A();
}
所以我们可以称之为单例。但是如果它有更多的字段就不能称为单例,因为单例模式只需要一个字段。我想这就是 Josua Bloch 的意思
众所周知,实现单例的最佳方法之一是使用 Enum
,因 Josh Bloch 而流行。
他说:
This approach is functionally equivalent to the public field approach, except that it is more concise, provides the serialization machinery for free, and provides an ironclad guarantee against multiple instantiation, even in the face of sophisticated serialization or reflection attacks. While this approach has yet to be widely adopted, a single-element enum type is the best way to implement a singleton.
这是否仍然适用于多元素 Enum
?每个元素不会只被实例化一次吗?
感知到的好处是有几个相似但不同的单例枚举,例如:每个元素注入不同的配置。
示例:
public enum Beatles {
george("George"),
john("John"),
paul("Paul"),
ringo("Ringo"),
;
private Beatles(final String name) {
this.name = name;
}
private final String name;
public String getName() {
return name;
}
}
您可以使用标记接口扩展单元素枚举范例以支持 "similar but different" 单例:
public interface MySingletonInterface {
void doFoo(Bar bar);
}
那么你的枚举可以实现接口:
public enum FirstSingletonEnum implements MySingleTonInterface {
...
@Override
public void doFoo(Bar bar) {
... //do something with bar
}
}
public enum SecondSingletonEnum implements MySingleTonInterface {
...
@Override
public void doFoo(Bar bar) {
... //do something slightly different with Bar
}
}
我觉得这是一种更简洁的方法,它将所有实现都塞进了一个枚举中。
就枚举内部的逻辑而言,我会非常小心地处理这个问题。总的来说,我认为枚举可以封装逻辑,只要该逻辑与每个枚举实例所代表的内容以及枚举作为一个整体所代表的内容紧密耦合即可。如果您正在执行诸如引入其他服务或与数据库等外部实体通信之类的操作,那么枚举可能不是正确的选择。
enum A {INSTANCE};
内部等同于
class A {
public static final A INSTANCE = new A();
}
所以我们可以称之为单例。但是如果它有更多的字段就不能称为单例,因为单例模式只需要一个字段。我想这就是 Josua Bloch 的意思