CSE Roadmap

Core tier · about 3 hours · 6 parts

Part 1 of 6 · 1 min read

Why this page comes fourth

In this module you will

  • Define a singleton and say when a class really should have one instance.
  • Write a singleton three ways in Java: public field, static factory and enum.
  • Show why a lazy singleton without a lock can build two objects.
  • Write a thread-safe lazy singleton with the holder idiom.
  • Explain the testing and global state problems and how injection avoids them.

Module 2 said a program usually needs one instance of a concrete factory. This page covers how to guarantee one instance, and when to avoid doing so. The sources are "Design Patterns" (GoF) chapter 3, "Effective Java" (EJ) Item 3 and Item 5, and "Java Concurrency in Practice" (JCIP) chapter 16. Code was compiled and run before it was written here. Numbers marked "made up" only show the idea.