Singleton without getter

The singleton pattern is often lazily used to make a class instance a global variable. This is bad practice for multiple reasons:

  • Typically no control over the creation of the instance (often lazy initialized)
  • No one remembers to release the memory used by the global instance when no longer in use
  • Getting the instance through a property is costly in loops
  • Singletons tend to become "god objects" that manage much more stuff than they should

When possible, particularly if the class can be stateless, try using a static class with static methods.

When you must use inheritance and manage states, create the class as a normal class without static fields, and put each instance in a wrapper for static, readonly access.

public static class Managers
{
    public static readonly TransformManager Transform = new TransformManager();
    public static readonly TagManager Tag = new TagManager();
    public static readonly HealthManager Health = new HealthManager();
    // And so on...
}

results matching ""

    No results matching ""