Implement a singleton class

๐Ÿ’ก Concept: Singleton Design Pattern

The singleton pattern restricts a class to a single instance, ensuring controlled access and lazy initialization.

๐Ÿ“˜ Quick Intro

Singleton ensures only one object is created and provides a global access point.

๐Ÿง  Analogy

Like a president managing a country, only one can hold the office at a time.

๐Ÿ”ง Technical Explanation

  • Private constructor prevents instantiation outside the class.
  • Static instance holds the single created object.
  • Thread safety achieved with locks or static initialization.
  • Lazy initialization delays instance creation until first use.
  • Ensures resource optimization and consistent state.

๐ŸŽฏ Use Cases

  • โœ… Logging frameworks.
  • โœ… Configuration managers.
  • โœ… Database connection pools.
  • โœ… Caching mechanisms.

๐Ÿ’ป Code Example


// Thread-safe singleton implementation
public sealed class Singleton {
    private static readonly Lazy<Singleton> instance = new Lazy<Singleton>(() => new Singleton());

    public static Singleton Instance => instance.Value;

    private Singleton() {
        // Private constructor
    }

    public void DoAction() {
        Console.WriteLine(""Singleton instance action."");
    }
}

โ“ Interview Q&A

Q1: What is a singleton pattern?
A: Restricts class to one instance.

Q2: Why use singleton?
A: Controlled access to resources.

Q3: How to ensure thread safety?
A: Use locks or Lazy<T>.

Q4: Can you inherit singleton?
A: Generally no, use sealed class.

Q5: When is lazy initialization useful?
A: To delay resource usage.

Q6: What problems does singleton solve?
A: Multiple instance conflicts.

Q7: Is singleton a design pattern?
A: Yes, a creational pattern.

Q8: Can singleton be broken?
A: With reflection or serialization.

Q9: Alternatives to singleton?
A: Dependency injection.

Q10: Is singleton thread-safe by default?
A: Not always, must implement safety.

๐Ÿ“ MCQs

Q1. What does singleton ensure?

  • Multiple instances
  • Single instance
  • No instance
  • Depends on usage

Q2. How to make singleton thread-safe?

  • Use locks or Lazy<T>
  • Ignore thread safety
  • Create multiple instances
  • Use static methods

Q3. Can singleton class be inherited?

  • Yes
  • No
  • Sometimes
  • Always

Q4. What is lazy initialization?

  • Immediate creation
  • Delay creation
  • Random creation
  • No creation

Q5. Why use singleton?

  • Speed
  • Resource control
  • Memory leaks
  • Testing

Q6. Is singleton a design pattern?

  • No
  • Yes
  • Maybe
  • Rarely

Q7. What can break singleton?

  • Locks
  • Reflection and serialization
  • Threading
  • Inheritance

Q8. Alternatives to singleton?

  • Singleton
  • Dependency injection
  • Factory pattern
  • Strategy pattern

Q9. Is singleton thread-safe by default?

  • Yes
  • No
  • Depends
  • Always

Q10. When is lazy initialization useful?

  • Never
  • Always
  • Delay resource usage
  • Immediately

๐Ÿ’ก Bonus Insight

The singleton pattern is essential for resource management and global state consistency.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ” Navigation

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: