From grimoire
Ensures a class has exactly one instance with a global access point. Useful for configuration stores, connection pools, and loggers where multiple instances would cause resource conflicts.
How this skill is triggered — by the user, by Claude, or both
Slash command
/grimoire:apply-singleton-patternThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Ensure a class has exactly one instance and provide a global access point to it.
Ensure a class has exactly one instance and provide a global access point to it.
Adopted by: Spring Framework (default bean scope is singleton — the most-used Java
framework, 65%+ of Java enterprise apps per JetBrains 2023 survey), Python's logging
module (each getLogger(name) returns the same Logger instance for that name), Go's
sync.Once (idiomatic Go singleton construction), and Java Runtime.getRuntime()
(JVM runtime singleton, in every JVM since Java 1.0).
Impact: GoF documents that uncontrolled global state causes initialization ordering
bugs and duplicate resource allocation. Connection pools (HikariCP, the most-used Java
connection pool) are singletons by necessity — two pools to the same DB double
connections and break max-connection limits.
Why best: The alternative — passing the shared instance everywhere via constructor
(dependency injection) — is architecturally cleaner and preferred when testability
matters. Singleton is correct when the instance is a true system-level resource (one
config, one log system, one thread pool) and DI wiring overhead is unjustified.
Sources: Gamma et al. (1994) pp. 127–136; Spring Framework documentation (Bean scopes);
Go sync.Once documentation; HikariCP design rationale
class Configuration:
_instance: "Configuration | None" = None
def __new__(cls) -> "Configuration":
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance._initialized = False
return cls._instance
def __init__(self):
if self._initialized:
return
self._data: dict = {}
self._initialized = True
__new__ (Python) or getInstance() (Java/C++) as the access point# All callers get the same instance
config1 = Configuration()
config2 = Configuration()
assert config1 is config2 # True
import threading
class ThreadSafeConfiguration:
_instance = None
_lock = threading.Lock()
def __new__(cls):
if cls._instance is None:
with cls._lock:
if cls._instance is None: # double-checked locking
cls._instance = super().__new__(cls)
return cls._instance
Double-checked locking avoids acquiring the lock on every access after initialization.
Python modules are singletons by default — imported once, cached in sys.modules:
# config.py — the module IS the singleton
_data: dict = {}
def get(key: str):
return _data[key]
def set(key: str, value):
_data[key] = value
# caller
import config
config.set("debug", True)
@classmethod
def _reset(cls):
cls._instance = None # allow re-initialization in tests
Mark this method as test-only. Production code never calls it.
Not guarding against subclassing. If the singleton class can be subclassed, a subclass can create a second instance. Use final (Java) or __init_subclass__ guards (Python) to prevent this.
Ignoring thread safety. A naive if not _instance: _instance = cls() has a race condition. Use sync.Once (Go), synchronized (Java), or double-checked locking (Python).
Using Singleton where Dependency Injection belongs. If the "singleton" needs to be swapped in tests, replaced in different environments, or extended, it's not a true singleton — it's a shared service that should be injected.
npx claudepluginhub jeffreytse/grimoire --plugin grimoireImplements singleton patterns in TypeScript via module-level singletons, class-based patterns, and WeakRef. Useful for shared database pools, loggers, and configs.
Covers 26 Gang of Four design patterns with PHP 8.3+ implementations, UML diagrams, and practical use cases.
Provides instructions for implementing the Prototype design pattern (cloning objects) in Python, JavaScript, and Java. Useful when object creation is expensive or runtime type is unknown.