Skip to content

Commit

Permalink
Restructured files & folder
Browse files Browse the repository at this point in the history
  • Loading branch information
bytewhisperer11 committed Dec 26, 2024
1 parent 91a28de commit 9826ea7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
File renamed without changes.
File renamed without changes.
45 changes: 45 additions & 0 deletions Creational/Singleton/Singleton.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Singleton Pattern

## Definition
The Singleton Pattern is a creational design pattern that ensures a class has only one instance and provides a global point of access to it. This pattern restricts the instantiation of a class to a single object, often used for managing shared resources or configurations.

## Intent
- To ensure that a class has only one instance throughout the application.
- To provide a global point of access to the single instance.

## Problem
- You need to control the instantiation of a class and ensure that only one instance exists, even in multi-threaded environments.
- You want to provide a consistent and centralized point of access to that single instance.

## Solution
- Make the class’s constructor private or protected to prevent direct instantiation from outside the class.
- Provide a static method that allows the client to access the single instance, ensuring that only one instance is ever created.

## Key Components
- **Singleton Class:** The class that ensures only one instance exists and provides global access to it.
- **Instance:** The single instance of the class, created lazily when required.
- **Static Access Method:** A method, typically static, that returns the single instance of the class.

## When to Use
- When you need to ensure that only one instance of a class exists, such as for managing shared resources like database connections, logging, or configuration settings.
- When the overhead of creating and managing multiple instances is unnecessary and undesirable.

## Benefits
- **Global Access:** The single instance can be accessed globally, making it easy to manage shared resources.
- **Controlled Instance Creation:** The pattern ensures that the object is created only when needed and maintains its unique instance.
- **Reduced Memory Usage:** As only one instance exists, it can help reduce memory usage and the overhead of multiple object creations.

## When to Avoid
- When the system requires multiple instances of the class, as this pattern limits instantiation to one.
- If the class is used in a multi-threaded environment without proper synchronization, it may lead to issues in creating the instance safely.
- When unit testing is challenging due to the global state maintained by the Singleton instance.

## Summary
The Singleton Pattern ensures that a class has only one instance and provides a global point of access to it. This pattern is useful for managing shared resources and configurations, but should be used carefully in multi-threaded applications and situations where multiple instances might be required.

## What to Prepare for Interview
- **Definition and Use Case:** Be able to explain the singleton pattern and provide practical examples of when to use it, such as managing global settings or logging.
- **Code Example:** Prepare a simple code example demonstrating the creation of a singleton class and accessing its instance.
- **Real-World Use Cases:** Understand how the Singleton pattern is applied in scenarios like logging, database connection pooling, or caching.
- **Comparison with Other Patterns:** Be ready to explain how the singleton pattern differs from other patterns like Factory Method (which creates multiple instances of products) and the Prototype Pattern (which creates new objects from a prototype).
- **Benefits and Trade-offs:** Discuss the advantages and limitations of using the singleton pattern, especially regarding thread safety, global state, and challenges in testing.

0 comments on commit 9826ea7

Please sign in to comment.