简体   繁体   中英

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 . And how do I set parameters from another class if the constructor is supposed to be 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.

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 .
  2. Keep the object in a private static variable.
  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).

In another class you call: 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.

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 . In the method you check if the instance if null , if so, you initialise it with the provided arguments.

Finally you return the 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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM