Design Patterns «Prev Next»

Singleton Pattern Exercise

Objective: Write a class that uses the Singleton pattern.

In this exercise, you will write a Panda class that guarantees exactly one instance can exist during program execution. You may answer in Java or C++.

Requirements

  1. Prevent callers from directly constructing Panda (use a non-public constructor).
  2. Provide a single access point (for example, getInstance() / instance()) that always returns the same object.
  3. Add one simple instance method (for example, speak(), eatBamboo(), or getName()) to show the object is usable.
  4. Optional but recommended: make the implementation safe for concurrent access (briefly note what you did).

Paste your solution into the text area and click Submit. Your answer will be displayed on the results page for review.