Singleton 00:00:00 Make a prediction You will distinguish having one instance in a defined scope from making its construction and later operations safe. Two threads both see the catalog has not been created yet. If they each call its constructor, how many catalogs can appear? Two. The check and the publication must obey one safe initialization mechanism. Returning a single shared reference afterward cannot undo work already performed twice. 00:00:28 The bell tower A town uses one bell tower to announce noon. Two builders each seeing an empty square could build two towers without a shared claim. Giving the builder one key protects construction, though it does not automatically keep the clock accurate thereafter. State the scope of the one instance and how it is safely published. Synchronizing creation does not synchronize every subsequent use of its mutable state. A Singleton is a class that controls how many objects of it exist: exactly one. It closes its constructor, keeps the object in a static field and offers one static method that returns it. Some things are shared by nature: settings, a connection pool, a thread pool, a printer queue. With two objects the program has two truths, or uses twice the connections. 00:01:18 Construct one shared catalog Two parts of a desktop program ask for its process-wide catalog. Loading a separate copy for each wastes work and gives divergent updates. Let one owner create the catalog once and return that instance to later callers. Decide explicitly whether eager or lazy construction suits startup. Start two calls at the same time in a test. One completed initialization must serve both. Safe creation alone does not make later mutations of the catalog thread safe. Release two callers at the same instant. Both should receive the same catalog instance only after its contents are initialized. Merely observing one pointer value is insufficient if another thread can see half-loaded data. Use the language's safe initialization mechanism. 00:02:08 A singleton cannot book every server's last seat Two application processes each have their own singleton inventory object. Both can still sell the same final seat because their memory is separate. Use an authoritative storage or coordination boundary to enforce that cross-process invariant. The singleton only controls access within its own scope. Start both processes and print their local instance identifiers. Each process can obey the singleton rule perfectly while both sell the same seat. The seat requires coordination in the shared storage system; changing how many catalog objects live in one worker cannot enforce it. A second process has a separate address space and can create its own singleton. The pattern cannot by itself guarantee global uniqueness across servers. If one instance is needed only for the lifetime of a request or test, inject it through that scope rather than storing it globally. Python logging retrieves a logger by name. Repeating the request for the same name returns the same logger, while a different name can identify a different logger. That is one instance per key. Notice how this differs from one instance for an entire process, and from one instance shared by many processes. 00:03:28 Try an interview follow-up Why check twice in double-checked locking? The first check avoids the lock for all calls after the first. The second is needed because another thread may have built the object while this one waited for the lock. Without the second check the lock only makes the two builds happen one after the other. A team uses a singleton to allocate invoice numbers. What happens after starting a second worker process? Each worker can create its own singleton and issue the same number. Keep the singleton only for process-local coordination if useful. Enforce globally unique numbers at a shared boundary with a uniqueness guarantee. 00:04:11 What to take with you A Singleton keeps its one object in a private static field, closes its constructor and hands the object out through one static method. The hard part is the first call: check and build must not be split by another thread, and the reference must be stored when the object is complete. Enum, holder class, static local and module let the language do that. One instance is often right. Global access is what hurts: hidden needs, tests that depend on each other, and a class that can never be two. So build one in main, pass it on, and remember that the pattern ends at the edge of the process.