JAVA DESIGN PATTERNS

Creational Patterns - Singleton Pattern

This is one of the most commonly used patterns. There are some instances in the application where we have to use just one instance of a particular class. Let’s take up an example to understand this.

A very simple example is say Logger, suppose we need to implement the logger and log it to some file according to date time. In this case, we cannot have more than one instances of Logger in the application otherwise the file in which we need to log will be created with every instance.

We use Singleton pattern for this and instantiate the logger when the first request hits or when the server is started.

package creational.singleton;

import org.apache.log4j.Priority;

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Properties;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.IOException;

public class Logger {

  private String fileName;
private Properties properties;
private Priority priority;

/**
* Private constructor
*/
private Logger() {
logger = this;
}

/**
* Level of logging, error or information etc
*
* @return level, int
*/
public int getRegisteredLevel() {
int i = 0;
try {
InputStream inputstream = getClass().getResourceAsStream("Logger.properties");
properties.load(inputstream);
inputstream.close();
i = Integer.parseInt(properties.getProperty("**logger.registeredlevel**"));
if(i < 0 || i > 3)
i = 0;
}
catch(Exception exception) {
System.out.println("Logger: Failed in the getRegisteredLevel method");
exception.printStackTrace();
}
return i;
}
/**
* One file will be made daily. So, putting date time in file
* name.
*
* @param gc GregorianCalendar
* @return String, name of file
*/
private String getFileName(GregorianCalendar gc) {
SimpleDateFormat dateFormat1 = new SimpleDateFormat("dd-MMM-yyyy");
String dateString = dateFormat1.format(gc.getTime());
String fileName = "C:\\prashant\\patterns\\log\\PatternsExceptionLog-" + dateString + ".txt";
return fileName;
}

/**
* A mechanism to log message to the file.
*
* @param p Priority
* @param message String
*/
public void logMsg(Priority p, String message) {
try {
GregorianCalendar gc = new GregorianCalendar();
String fileName = getFileName(gc);
FileOutputStream fos = new FileOutputStream(fileName, true);
PrintStream ps = new PrintStream(fos);
SimpleDateFormat dateFormat2 = new SimpleDateFormat("EEE, MMM d, yyyy 'at' hh:mm:ss a");
ps.println("<"+dateFormat2.format(gc.getTime())+">["+message+"]");
ps.close();
}
catch (IOException ie) {
ie.printStackTrace();
}
}
/**
* this method initialises the logger, creates an object
*/
public static void initialize() {
logger = new Logger();
}

// singleton - pattern
private static Logger logger;
public static Logger getLogger() {
return logger;
}

}// End of class

Difference between static class and static method approaches:
One question which a lot of people have been asking me. What’s the difference between a singleton class and a static class? The answer is static class is one approach to make a class “Singleton”.

We can create a class and declare it as “final” with all the methods “static”. In this case, you can’t create any instance of class and can call the static methods directly.

Example:

final class Logger {
//a static class implementation of Singleton pattern
static public void logMessage(String s) {
System.out.println(s);
}
}// End of class

//==============================
public class StaticLogger {
public static void main(String args[]) {
Logger.logMessage("This is SINGLETON");
}
}// End of class

The advantage of this static approach is that it’s easier to use. The disadvantage of course is that if in future you do not want the class to be static anymore, you will have to do a lot of recoding.


Patterns
Creational Patterns
Factory Pattern
Abstract Factory Pattern
Singleton Pattern
Builder Pattern
Prototype Pattern
Structural Patterns
Adapter Pattern
Bridge Pattern
Composite Pattern
Decorator Pattern
Facade Pattern
Flyweight Pattern
Proxy Pattern
Behavioral Patterns
Chain of Responsibility Pattern
Command Pattern
Interpreter Pattern
Iterator Pattern
Mediator Pattern
Momento Pattern
Observer Pattern

State Pattern
Strategy Pattern
Template Pattern

Visitor Pattern
Use of this website constitutes acceptance of the AllAppLabs.com Terms and Conditions