[英]How do I make sure to initialize only one Instance but from another class?
I recently read about the singleton principle but I dont quite get how I can initialize this single instance of a class form another class if the constructor is private
. 我最近阅读了有关singleton原理的信息,但是如果构造函数是private
话,我还不太了解如何从另一个类初始化这个类的单个实例。 And how do I set parameters from another class if the constructor is supposed to be private
? 如果构造函数应该是private
,如何从另一个类设置参数?
public class Player {
String name;
Position p;
Inventory i;
private Player(String name, Position p, Inventory i){
}
static {
instance =
}
public static Player getPlayer(){
return instance;
}
}
You should create a private static Player
variable, and in your getPlayer()
method or in the static block create the object and assign to the above variable if it's null. 您应该创建一个private static Player
变量,并在getPlayer()
方法或静态块中创建对象,然后将其分配给上述变量(如果为空)。
public static Player getPlayer(){
if(player == null){
player = new Player("name", p, i);
}
return player;
}
this way you create only a single instance. 这样,您只能创建一个实例。
Simple rules, 简单的规则
private
. 该类的构造函数应为private
。 private static
variable. 将对象保留在private static
变量中。 synchronized
to the getter to make it thread safe (optional). 您还可以将synchronized
添加到吸气剂以使其线程安全(可选)。 In another class you call: Player.getPlayer()
在另一个类中,您调用: Player.getPlayer()
It always return the only one static instance of your class. 它总是返回类的唯一静态实例。 The constructor is private so other class cannot initialize your class via constructor. 构造函数是私有的,因此其他类无法通过构造函数初始化您的类。
The only way to obtain your Player
instance is via the static method Player.getPlayer()
hence it's singleton. 获取您的Player
实例的唯一方法是通过静态方法Player.getPlayer()
因此它是单例的。
public class Player {
String name;
Position p;
Inventory i;
private static final Player instance = new Player(.....your argument....);
private Player(String name, Position p, Inventory i){
}
public static Player getPlayer(){
return instance;
}
}
Basically, with this approach you hide the constructor ( private
) and exposes a static method for getting the instance
. 基本上,使用这种方法可以隐藏构造函数( private
),并公开用于获取instance
的静态方法。 In the method you check if the instance
if null
, if so, you initialise it with the provided arguments. 在方法中,检查instance
是否为null
,如果为null
,则使用提供的参数对其进行初始化。
Finally you return the instance
. 最后,您返回instance
。
Notice here if you call the getPlayer
method more than once, the instance will be created (and will be the same in further calls of method getPlayer
) with the arguments provided the first time the static method was called. 请注意 ,如果您多次调用getPlayer
方法,则将使用第一次调用静态方法时提供的参数创建实例(并且在进一步调用getPlayer
方法时将是相同的)。
public class Player {
String name;
Position p;
Inventory i;
private static Player instance;
private Player(String name, Position p, Inventory i){
// ...
}
public static Player getPlayer(String name, Position p, Inventory i){
if (instance == null) {
instance = new Player(name, p, i);
}
return instance;
}
}
Additionally, if you want to use the singleton pattern properly, you should not set the attributes (no setters methods) once the instance is created. 此外,如果要正确使用单例模式,则在创建实例后就不应设置属性(无设置方法)。
只需声明一个私有的static final INSTANCE
字段为
private static final INSTANCE = new Person(name, p, i);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.