5.
Design and Implement a Logger class ensuring a single instance throughout the
Application.
public class Main {
// Logger class following Singleton Pattern
static class Logger {
// Single private instance
private static Logger instance;
// Private constructor (prevents instantiation)
private Logger() {}
// Public method to provide access to the single instance
public static Logger getInstance() {
if (instance == null) {
instance = new Logger();
return instance;
// Logging method
public void log(String message) {
[Link]("[LOG] " + message);
}
// Test application
public static void main(String[] args) {
// First call - creates a new Logger instance
Logger logger1 = [Link]();
[Link]("Application started.");
// Second call - returns the SAME Logger instance
Logger logger2 = [Link]();
[Link]("Performing some operation...");
// Check if both loggers are the same
if (logger1 == logger2) {
[Link]("Both logger references point to the SAME instance.");
} else {
[Link]("Different instances created (error in Singleton!).");