0% found this document useful (0 votes)
18 views2 pages

Program 5

The document presents a Logger class implemented using the Singleton Pattern to ensure a single instance throughout the application. It includes a private constructor and a public method to access the instance, along with a logging method. A test application demonstrates the functionality by creating two logger references and confirming they point to the same instance.

Uploaded by

Sri harish
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

Program 5

The document presents a Logger class implemented using the Singleton Pattern to ensure a single instance throughout the application. It includes a private constructor and a public method to access the instance, along with a logging method. A test application demonstrates the functionality by creating two logger references and confirming they point to the same instance.

Uploaded by

Sri harish
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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!).");

You might also like