Singleton Pattern

noun

say it: “SING-gul-tun PAT-ern” /ˈsɪŋɡəltən ˈpætərn/

A design pattern where only one object of a class exists, shared by the whole program.

What is Singleton Pattern?

The Singleton pattern makes sure a class has only one object for the whole program, like one shared settings object. Use it sparingly: it can make testing harder.

public sealed class AppSettings
{
    public static AppSettings Instance { get; } = new();
    private AppSettings() { }
}

In a sentence

  • “The cart service follows the Singleton pattern: one cart for the whole app.”

Level: Freelancer · More OOP terms at this level

Updated