/*******************************************************************************
* Companion code for the book "Introduction to Software Design with Java",
* 2nd edition by Martin P. Robillard.
*
* Copyright (C) 2022 by Martin P. Robillard
*
* This code is licensed under a Creative Commons
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
*
* See http://creativecommons.org/licenses/by-nc-nd/4.0/
*
*******************************************************************************/
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()
.