Classes and Singleton pattern
Course: Singleton Pattern, Backend State management and Pub Subs - chapter 4
Let’s create a class that
- Stores games
- Exposes functions that let you mutate the state
interface Game { id: string; whitePlayer: string; blackPlayer: string; moves: string[];}
export class GameManager { private games: Game[] = [];
public addGame(game: Game) { this.games.push(game); }
public getGames() { return this.games; }
// e5e7 public addMove(gameId: string, move: string) { const game = this.games.find(game => game.id === gameId); if (game) { game.moves.push(move); } }
public logState() { console.log(this.games); }}Bad approach
Section titled “Bad approach”Create saparate instance of GameManager in every file that needs it
GameManager.ts
interface Game { id: string; whitePlayer: string; blackPlayer: string; moves: string[];}
export class GameManager { private games: Game[] = [];
public addGame(game: Game) { this.games.push(game); }
public getGames() { return this.games; }
// e5e7 public addMove(gameId: string, move: string) { const game = this.games.find(game => game.id === gameId); if (game) { game.moves.push(move); } }}logger.ts
import { GameManager } from "./GameManager";
const gameManager = new GameManager();
export function startLogger() { setInterval(() => { gameManager.logState(); }, 4000)}index.ts
import { GameManager } from "./GameManager";import { startLogger } from "./logger";
const gameManager = new GameManager();
startLogger();
setInterval(() => { gameManager.addGame({ id: Math.random().toString(), "whitePlayer": "harkirat", "blackPlayer": "jaskirat", moves: [] })}, 5000)Slightly Better approach
Section titled “Slightly Better approach”Export a single instance of gameManager from GameManager.ts and use it everywhere
Even better approach - Singleton Pattern
Section titled “Even better approach - Singleton Pattern”Completely prevent any developer from ever creating a new instance of the GameManager class
Static attributes -
Section titled “Static attributes -”In JavaScript, the keyword **static** is used in classes to declare static methods or static properties. Static methods and properties belong to the class itself, rather than to any specific instance of the class. Here’s a breakdown of what this means
Example of a class with static attributes
class Example { static count = 0;
constructor() { Example.count++; // Increment the static property using the class name }}
let ex1 = new Example();let ex2 = new Example();console.log(Example.count); // Outputs: 2interface Game { id: string; whitePlayer: string; blackPlayer: string; moves: string[];}
export class GameManager { private static instance: GameManager; // Create a static instance of the class private games: Game[] = [];
private constructor() { // Private constructor ensures that a new instance cannot be created from outside }
public static getInstance(): GameManager { if (!GameManager.instance) { GameManager.instance = new GameManager(); } return GameManager.instance; } // ... other methods}
// Usage GameManager.getInstance().addGame()GameManager.ts
interface Game { id: string; whitePlayer: string; blackPlayer: string; moves: string[];}
export class GameManager { private static instance: GameManager; // Create a static instance of the class private games: Game[] = [];
private constructor() { // Private constructor ensures that a new instance cannot be created from outside }
public static getInstance(): GameManager { if (!GameManager.instance) { GameManager.instance = new GameManager(); } return GameManager.instance; }
public addGame(game: Game) { this.games.push(game); }
public getGames() { return this.games; }
public addMove(gameId: string, move: string) { const game = this.games.find(game => game.id === gameId); if (game) { game.moves.push(move); } }
public logState() { console.log(this.games); }}logger.ts
import { GameManager } from "./GameManager";
export function startLogger() { setInterval(() => { GameManager.getInstance().logState(); }, 4000)}index.ts
import { GameManager } from "./GameManager";import { startLogger } from "./logger";
startLogger();
setInterval(() => { GameManager.getInstance().addGame({ id: Math.random().toString(), "whitePlayer": "harkirat", "blackPlayer": "jaskirat", moves: [] })}, 5000)Try creating a new instance of the GameManager class. Notice it wont let you.