Singleton
medium 60 minOne object and one way to reach it.
Watch lesson4:52
Check yourself
One question at a time. Each answer explains why.
Question 1 of 14
What does the Singleton pattern promise?
Why is the constructor private?
What is lazy initialization?
In double-checked locking somebody removes the second check. What happens?
A Java checked lock has a field without volatile. What can a reader get?
Which Java form is closed against reflection and serialization?
Which one should not be a Singleton?
Six processes each use a pool Singleton with 20 connections. The database allows 100. What happens?
Test B fails only when test A ran before it. Both use Flags.get_instance(). Best repair?
What does the holder class rely on?
A Python class returns its one object from __new__ and sets self.url in __init__. Someone changes url, then calls Config() again. What is url?
How does a Singleton differ from static methods only?
Host and password of a database change together while requests run. What keeps readers consistent?
Only one of ten processes may run the nightly job. Which tool?
Pick an answer to see why.
Not quite. Both promises belong to the definition. The second one causes most of the trouble.
Not quite. Otherwise anyone could write new, and the static field would be a suggestion.
Not quite. Nothing is paid until somebody asks.
Not quite. The lock only puts the two builds in a row.
Not quite. The first check reads without a lock, so nothing orders the writes of the constructor for that reader.
Not quite. The runtime creates enum constants, and a stream holds only their name.
Not quite. There is one per user. One shared cart shows items to strangers.
Not quite. One per process: 6 x 20 = 120.
Not quite. The shared state is the cause. An order only hides it.
Not quite. The inner class is not initialized until getInstance names it.
Not quite. Python calls __init__ on the object that __new__ returned, every time.
Not quite. That is also the way out: an object can be injected later.
Not quite. Two safe setters still leave a moment in between. A single reference has none.
Not quite. Objects end at the process. The number stops a leader that slept and woke up.
Flashcards
Say your answer, then reveal it.
Card 1 of 14
Three parts of a Singleton?
Private static field, private constructor, public static method.
Say your answer, then reveal it.
What is the race in the lazy version?
Two threads both see an empty field and both build.
Say your answer, then reveal it.
Purpose of the first check?
Avoid the lock on all calls after the first.
Say your answer, then reveal it.
Purpose of the second check?
Somebody may have built it while this thread waited for the lock.
Say your answer, then reveal it.
Purpose of volatile in Java?
Whoever sees the reference sees the finished object.
Say your answer, then reveal it.
Holder class?
A private inner class with a static final instance. Lazy, locked by class initialization.
Say your answer, then reveal it.
Why an enum in Java?
Made once by the runtime, closed against serialization and reflection.
Say your answer, then reveal it.
Singleton in C++?
A static local variable, with copy deleted.
Say your answer, then reveal it.
Singleton in Python or TypeScript?
A module: it runs once.
Say your answer, then reveal it.
Trap of __new__ in Python?
__init__ runs again on every call and resets the fields.
Say your answer, then reveal it.
How many instances with ten processes?
Ten. For one per system use a lease or a conditional update.
Say your answer, then reveal it.
Main criticism?
Global access hides dependencies and lets state leak between tests.
Say your answer, then reveal it.
The cure?
Build one in main and pass it on. The class stays ordinary.
Say your answer, then reveal it.
How to change several settings at once?
Replace an immutable snapshot. A request takes the reference once.
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.