This project demonstrates the Singleton Design Pattern using a Logger example in Java.
The purpose of this repository is to help students and beginners understand Singleton:
- Why it is needed
- How it works
- How it is applied in real-world systems (like logging)
The Singleton Design Pattern is a creational design pattern that ensures:
- Only one instance of a class exists throughout the application
- That instance is globally accessible
In simple terms:
One class β One object β Shared everywhere
Logging is a shared service used by many parts of an application.
- Multiple Logger objects created
- Multiple log files opened
- Inconsistent log messages
- File access conflicts
- Higher memory usage
Use Singleton so that all parts of the application use the same Logger instance.
- Logger
- Database connections
- Configuration managers
- Cache systems
- Thread pools
A Singleton class follows 3 essential rules:
-
Private Constructor
Prevents creating objects usingnew. -
Private Static Instance Variable
Holds the single instance of the class. -
Public Static Access Method
Returns the same instance every time it is called.