简体   繁体   English

如何确保仅初始化一个实例,但从另一个类初始化?

[英]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, 简单的规则

  1. The constructor of the class should be private . 该类的构造函数应为private
  2. Keep the object in a private static variable. 将对象保留在private static变量中。
  3. Create a getter for the singleton object (no setters). 为单例对象创建一个吸气剂(无设置器)。
  4. You can also add 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.

相关问题 如何确保仅一个指定声音的实例在循环,如何停止循环? - How do I make sure only 1 instance of a specified Sound is looping and how do I stop it from looping? 如何确保只能执行我的程序的一个实例? - How do I make sure only one instance of my program can be executed? 如何确保一次只调用一个servlet(不是实例)? - how to I make sure that only one servlet(not instance) is call at a time? 如何确保在 Java 中只选择了一个 JRadioButton? - How Do I Make Sure Only One JRadioButton Is Selected In Java? 如何确保仅启动一个线程 - How do I make sure only one thread gets started 如何确保JVM中只有一个类实例? - How to make sure that there is just one instance of class in JVM? 我如何初始化另一个类的数组? - How do i initialize array of a another class? 对于 Java,如果我想做一个 if 语句以确保输入只包含一个字符,我应该如何进行? - For Java, if I want to do an if statement to make sure the input only takes in one character, how should I proceed? 如何在 testNG 中实现从一个测试类到另一个测试类的控制流? - How do I make the control flow from one test class to another test class in testNG? 如何确保输入不是 null 并且只输入了一个字符? - How do I make sure that the input is not null and that only one character is being entered?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM