Retiring the Singleton Pattern: Concrete Suggestions for What to use Instead
summary
Shows how to remove hidden singletons by introducing small wrapper types (e.g. comWrapper) passed as parameters with a default obtained via lazy factory like get_default_comms, turning opaque global access into constructor or parameter injection. Uses virtuals or std::function/std::ref to enable mocks, groups many dependencies in a service struct or with templates/type erasure, and preserves ABI by keeping single-parameter dispatchors that forward to two-parameter implementations. Implementation runs at Bloomberg, fixes copy/fork socket bugs, and makes formerly untestable code unit-testable.
AI-written summary · about
chapters (12)
- 0:00Motivation for retiring
- 4:50Defining the problem
- 6:34Analyzing the singleton pattern
- 11:56Singleton or not: a quiz
- 15:05Refactoring with dependency injection
- 24:30Modern C++ approaches
- 32:11Preserving compatibility
- 34:03Initialization strategies
- 37:53Phased introduction
- 40:57Interdependent dependencies
- 47:10Grouping multiple dependencies
- 54:04Final conclusions and Q&A
related talks
01h 16mRetiring the Singleton Pattern, Concrete Suggestions for What to Use Instead
05mOn using singletons in C++
58mA Practical Guide to Loose Coupling
01h 02mRefactoring C++ Code for Unit testing with Dependency Injection
01mTalk Preview - The Surprising Power of Small Wrapper Classes in C++
01h 28mLaw of Demeter: A Practical Guide to Loose Coupling
01h 29mDependency Injection in C++ - A Practical Guide
01h 20mHiding your Implementation Details is Not So Simple
