/**
* Copyright (C) 2022 by Martin Robillard. See https://codesample.info/about.html
*/
package e2.chapter4;
/**
* Stub for a GameModel class that is an application
* of the Singleton design pattern.
*/
public class GameModel {
private static final GameModel = new GameModel();
GameModel() {}
public static GameModel instance() {
return INSTANCE;
}
}
For the singleton pattern to be enforced, the constructor must be private, so that client can't create new instances of the singleton.
For the singleton pattern to be enforced, the constructor must be private, so that client can't create new instances of the singleton.
The only instance of GameModel
that can exist is created once, when the static field
is initialized. Thereafter, the singleton instance can be accessed via method instance()
.
The only instance of GameModel
that can exist is created once, when the static field
is initialized. Thereafter, the singleton instance can be accessed via method instance()
.