使用 POJO 进行控制反转和依赖注入
Inversion of Control and Dependency Injection with POJO
有些人使用 Enum 而不是 class 来创建对象并将其注入代码。例如
public enum EnumSample {
INSTANCE;
private String name = "Sample Enum";
private String version = "1";
public String getName() {
return this.name;
}
public String getVersion() {
return this.version;
}
}
在class中:
public class MyClass {
public static void main(String[] args) {
new App();
}
}
public class App {
private EnumSample enumSample = EnumSample.INSTANCE;
public App() {
System.out.printf("%s - %s",enumSample.getName(), enumSample.getVersion());
}
}
所以它是正确的吗?因为在 Oracle 网站 Java 的文档中他们说:
From Java documents -
You should use enum types any time you need to represent a fixed set of constants. That includes natural enum types such as the planets in our solar system and data sets where you know all possible values at compile time—for example, the choices on a menu, command line flags, and so on.
我只是想知道这种方式是否正确,有没有更好的模式?请考虑该版本在这里只是一个例子...
我知道 Enum 对于 Singelton 是正确的...我只是想找到没有 Enum 的其他方法,如果有的话请与我分享...谢谢
Java中enum
实际上有两个主要用例:
(1) 定义一组固定常量,如 Java 文档中直接提到的那样。
(2) enum
也可用于安全地(线程)为 class 创建单例实例(即,无需显式使用 synchronize
等)。您可以参考 here 关于这个主题。
在你的 EnumSample
中,你实际上是在创建 EnumSample
的一个且只有一个对象,它可以用变量 INSTANCE
引用(上面的用例 2)。
I got that Enum is correct for Singelton ones. I just want to find the
other ways to do these without Enum.
在Java中,有几种方法可以以线程安全的方式创建单例实例,其中一种如下所示,它使用static
final
INSTANCE
使用 private
构造函数:
public class Sample {
//Create a static final object
private static final Sample INSTANCE = new Sample();
//private constructor, so this class can't instantiated from outside
private Sample() {
}
//Use the getInstance() static method which returns same instance always
public static Sample getInstance() {
return INSTANCE;
}
private String name = "Sample Enum";
private String version = "1";
public String getName() {
return this.name;
}
public String getVersion() {
return this.version;
}
}
public class App {
public App() {
System.out.printf("%s - %s",Sample.getInstance().getName(),
Sample.getInstance().getVersion());
}
}
其他安全创建单例的方法可以参考here
有些人使用 Enum 而不是 class 来创建对象并将其注入代码。例如
public enum EnumSample {
INSTANCE;
private String name = "Sample Enum";
private String version = "1";
public String getName() {
return this.name;
}
public String getVersion() {
return this.version;
}
}
在class中:
public class MyClass {
public static void main(String[] args) {
new App();
}
}
public class App {
private EnumSample enumSample = EnumSample.INSTANCE;
public App() {
System.out.printf("%s - %s",enumSample.getName(), enumSample.getVersion());
}
}
所以它是正确的吗?因为在 Oracle 网站 Java 的文档中他们说:
From Java documents -
You should use enum types any time you need to represent a fixed set of constants. That includes natural enum types such as the planets in our solar system and data sets where you know all possible values at compile time—for example, the choices on a menu, command line flags, and so on.
我只是想知道这种方式是否正确,有没有更好的模式?请考虑该版本在这里只是一个例子...
我知道 Enum 对于 Singelton 是正确的...我只是想找到没有 Enum 的其他方法,如果有的话请与我分享...谢谢
Java中enum
实际上有两个主要用例:
(1) 定义一组固定常量,如 Java 文档中直接提到的那样。
(2) enum
也可用于安全地(线程)为 class 创建单例实例(即,无需显式使用 synchronize
等)。您可以参考 here 关于这个主题。
在你的 EnumSample
中,你实际上是在创建 EnumSample
的一个且只有一个对象,它可以用变量 INSTANCE
引用(上面的用例 2)。
I got that Enum is correct for Singelton ones. I just want to find the other ways to do these without Enum.
在Java中,有几种方法可以以线程安全的方式创建单例实例,其中一种如下所示,它使用static
final
INSTANCE
使用 private
构造函数:
public class Sample {
//Create a static final object
private static final Sample INSTANCE = new Sample();
//private constructor, so this class can't instantiated from outside
private Sample() {
}
//Use the getInstance() static method which returns same instance always
public static Sample getInstance() {
return INSTANCE;
}
private String name = "Sample Enum";
private String version = "1";
public String getName() {
return this.name;
}
public String getVersion() {
return this.version;
}
}
public class App {
public App() {
System.out.printf("%s - %s",Sample.getInstance().getName(),
Sample.getInstance().getVersion());
}
}
其他安全创建单例的方法可以参考here