WorkspaceLow-Level Design
← Course overview

Course navigation

On this page

Low-Level Design / Creational Patterns

pattern · 12 MIN READ

Singleton

A singleton controls access to one shared instance within a specified scope. It does not create one instance across browser tabs, operating-system processes, or distributed deployments.

Reading completion is independent of quizzes and challenges.

What you’ll understand

  • A singleton controls access to one shared instance within a specified scope. It does not create one instance across browser tabs, operating-system processes, or distributed deployments.
  • Keep instance creation in one access point and return the same configuration identity on repeated access.
  • Evaluate the tradeoff: Global mutable access can hide dependencies, leak state between tests, and complicate concurrency. Prefer explicit dependency injection when a shared instance does not need global discovery.

Useful first: Classes and Objects

The idea and when to recognize it

A singleton controls access to one shared instance within a specified scope. It does not create one instance across browser tabs, operating-system processes, or distributed deployments.

  • Requirement: Two components read the same process-local configuration object.
  • Change to anticipate: Update the configuration through one reference and observe the update through the other.
  • Design goal: Keep instance creation in one access point and return the same configuration identity on repeated access.

Start with the concrete problem

Two components read the same process-local configuration object.

Update the configuration through one reference and observe the update through the other.

Direct implementationRevised design
Creating a new configuration for every access violates the promised shared identity.Keep instance creation in one access point and return the same configuration identity on repeated access.
The baseline case can hide the problem. Compare the extension and boundary fixtures before accepting this design.Global mutable access can hide dependencies, leak state between tests, and complicate concurrency. Prefer explicit dependency injection when a shared instance does not need global discovery.

Responsibilities and contracts

Keep instance creation in one access point and return the same configuration identity on repeated access.

  • Configuration: Stores scoped shared settings. Public operations: getInstance(), value
  • Client: Reads the shared setting. Public operations: read()

Read the design

Select a participant to inspect its responsibility and public operations. Follow the labeled relationships; an arrow indicates direction, while the legend distinguishes ownership, substitution, and temporary use.

Preparing the design walkthrough…

Follow the interaction

Choose a supplied workload and compare the direct and revised implementations. Step through the same operation in both languages. Object values are recorded after each highlighted statement; previous steps remain unchanged.

Preparing the design walkthrough…

Implementation and expected output

Read the complete example alongside its input and expected output. The implementation selector changes the displayed program; the language selector preserves your current scenario and playback position.

Preparing the design walkthrough…

Try the idea in another setting

A process-local registry can provide one shared lookup table, while each test constructs a fresh scope.

  • Name the new client contract before adapting the example.
  • Identify which state belongs to each object and who controls its lifetime.
  • Write one normal case and one boundary case before extending the design.

Tradeoffs and interview takeaways

Global mutable access can hide dependencies, leak state between tests, and complicate concurrency. Prefer explicit dependency injection when a shared instance does not need global discovery.

Common mistake: Creating a new configuration for every access violates the promised shared identity.

  • Explain the choice: Keep instance creation in one access point and return the same configuration identity on repeated access.
  • Challenge your choice: Global mutable access can hide dependencies, leak state between tests, and complicate concurrency. Prefer explicit dependency injection when a shared instance does not need global discovery.
  • Describe a requirement that would make the simpler implementation sufficient.

Design challenge and knowledge check

Preparing the design walkthrough…

Make it your own

Saved on this device · 0/20,000

Reading completion is independent of quizzes and challenges.