DESIGN PATTERN PROBLEMS
Because design patterns are so abstract, you can expect a lot of variation in the types of questions that are asked.
Singleton Implementation
The Singleton pattern ensures that at most one instance of the logging class exists at any given time. The easiest way to do this is to make the constructor private and initialize the single instance within the class. Here’s a Java implementation of the logger:
// Implements a simple logging class using a singleton.
public class Logger {
// Create and store the singleton.
private static final Logger instance = new Logger();
// Prevent anyone else from creating this class.
private Logger(){
}
// Return the singleton instance.
public static Logger getInstance() { return instance; }
// Log a string to the console...