Singleton: Why & How

Hands-on practice for this lecture. Work through the exercises and quizzes to reinforce what you've learned.

1

Exercise 1 of 2

Lab: Singleton Memory Visualizer

A high-fidelity memory simulation. Monitor the Heap in real-time as you request instances, and witness the catastrophic impact of race conditions and security breaches.

System Core v2.0

Kernel Logs
System Idle // Waiting for Instruction

Memory Visualizer

Segment: HEAP_ALLOC_RANGE_0x00

Allocated: 0

Peak: 64.2 MB

Heap Unallocated

Technical Analysis

A correct Singleton should only ever result in one address in memory.Race conditions happen when thread synchronization fails.Security attacks (Reflection/Serialization) bypass the language rules to force-create new memory segments.

2

Exercise 2 of 2

Lab: The Lazy Initialization Ritual

Write the code for a Lazy Singleton. Choose the right checks and assignments to ensure "The One and Only" instance is born correctly.

Implement getInstance()

public static Database getInstance() {
Step 1: Check if instance exists
Step 2: Create instance if null
Step 3: Return the shared instance
/* ... return logic ... */
}

Select the correct logic

Complete the lazy initialization ritual.

Practice: Singleton: Why & How — Interactive Exercises | Durgesh Rai