|
1 |
| -## Getting Started |
| 1 | +# Singleton Design Pattern |
2 | 2 |
|
3 |
| -Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. |
| 3 | +## Overview |
4 | 4 |
|
5 |
| -## Folder Structure |
| 5 | +The Singleton design pattern ensures that a class has only one instance and provides a global point of access to that instance. It's commonly used when exactly one object is needed to coordinate actions across the system, such as managing a shared resource or controlling access to a limited resource. |
6 | 6 |
|
7 |
| -The workspace contains two folders by default, where: |
| 7 | + |
8 | 8 |
|
9 |
| -- `src`: the folder to maintain sources |
10 |
| -- `lib`: the folder to maintain dependencies |
| 9 | +### Key Points |
11 | 10 |
|
12 |
| -Meanwhile, the compiled output files will be generated in the `bin` folder by default. |
| 11 | +- **Single Instance**: Singleton classes have only one instance that is globally accessible throughout the application. |
| 12 | +- **Global Access**: Provides a global access point to the single instance, allowing it to be easily accessed by other parts of the code. |
| 13 | +- **Lazy Initialization**: Singleton instances can be created either eagerly (at the start) or lazily (when requested), depending on the implementation. |
| 14 | +- **Thread Safety**: Various approaches exist to ensure thread safety during the creation of Singleton instances in multi-threaded environments. |
13 | 15 |
|
14 |
| -> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. |
| 16 | +## How It Works |
15 | 17 |
|
16 |
| -## Dependency Management |
| 18 | +### Eager Implementation |
17 | 19 |
|
18 |
| -The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). |
| 20 | +```java |
| 21 | +public class Singleton { |
| 22 | + private static final Singleton instance = new Singleton(); |
| 23 | + |
| 24 | + private Singleton() {} |
| 25 | + |
| 26 | + public static Singleton getInstance() { |
| 27 | + |
| 28 | + return instance; |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | +### Lazy Implementation |
| 33 | + |
| 34 | +```java |
| 35 | +public class Singleton { |
| 36 | + private static final Singleton instance; |
| 37 | + |
| 38 | + private Singleton() {} |
| 39 | + |
| 40 | + public static Singleton getInstance() { |
| 41 | + if(instance == null) { |
| 42 | + instance = new Singleton(); |
| 43 | + } |
| 44 | + return instance; |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +### Thread-Safe Implementation (Double-Checked Locking) |
| 50 | + |
| 51 | +```java |
| 52 | +public class Singleton { |
| 53 | + private volatile static Singleton instance; |
| 54 | + |
| 55 | + private Singleton() {} |
| 56 | + |
| 57 | + public static Singleton getInstance() { |
| 58 | + if (instance == null) { |
| 59 | + synchronized (Singleton.class) { |
| 60 | + if (instance == null) { |
| 61 | + instance = new Singleton(); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + return instance; |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +### Use Cases |
| 71 | +- **Resource Managers:** Database connection pools, file managers, etc., where a single instance ensures efficient resource utilization. |
| 72 | +- **Configuration Settings:** Holding configuration settings that need to be accessed throughout the application. |
| 73 | + |
| 74 | +### Best Practices |
| 75 | +- **Lazy Initialization:** Use lazy initialization for better resource management unless eager initialization is specifically required. |
| 76 | +- **Thread Safety:** Ensure thread safety in multi-threaded environments when implementing Singleton. |
| 77 | +- **Consider Alternatives:** Consider alternatives like dependency injection frameworks for managing single instances. |
| 78 | + |
| 79 | +### Breaking the Singleton Pattern |
| 80 | +The Singleton pattern can be broken if not implemented correctly: |
| 81 | +1. **Reflection:** Using reflection, it's possible to access the private constructor of a Singleton class and create another instance. |
| 82 | +2. **Serialization and Deserialization:** During deserialization, if not handled properly, it might create a new instance, violating the Singleton pattern. |
| 83 | +3. **Synchronization Issues:** Improper synchronization in a multi-threaded environment can lead to multiple instances being created due to race conditions. |
| 84 | + |
| 85 | +## Contributor : |
| 86 | + |
| 87 | +<table> |
| 88 | + <tr> |
| 89 | + <td align="center"><a href="https://github.com/DhirajGadekar"><img src="https://avatars.githubusercontent.com/u/111908836?v=4" width="100px;" alt="Dhiraj Gadekar"/><br/><sub><b>Dhiraj Gadekar</b></sub></a><br/> |
| 90 | +</tr> |
| 91 | +</table> |
| 92 | +Contributions are welcome! If you have any suggestions, improvements, or additional examples related to the Singleton pattern, feel free to open an issue or submit a pull request. |
| 93 | + |
| 94 | +## Feedback |
| 95 | + |
| 96 | +If you have any feedback, please reach out to us at **<a href="https://mail.google.com/mail/u/3/#inbox?compose=CllgCJTHWNbjZQFnRQlzTNlRVSXcTdxfZVrbtCvTZWPTxTWwgDHTpnckBglPXzNWwkPgMBkrZSq" target="_blank">Email</a>** |
0 commit comments