Magazine
 
Creational Design Patterns

Singleton Pattern:

The singleton design pattern deals with one and only one instance of an object that encapsulates the control of the object from a common place. There are various ways of achieving this pattern.

For example, create an exception that is thrown by a class if the class is instantiated more than once. Next declare a boolean variable and make it static so that it can be shared among all the instances of a class. Initialize this static variable inside the constructor of this class. The constructor would throw an exception if it is initialized second time. It is the best technique of using the singleton pattern. But take care to set the variable whenever destroying during the finalize method call.

Another approach of creating Singleton
method includes a static method and
private constructor in a class. Declaring the constructor as private ensures that the instance variable can be created only from inside the method of the class. The static method sets the variable as boolean that indicates the creation of the instance and returns an instance.

Benefits: Singleton pattern controls access to unique instances, reduce name space, permits a variable number of instances, allows refinement of operations and representations, and provides more flexibility than class operations.

Usage: These are used in those places where there is only one instance of a class. The Singleton pattern is mostly used in multi-threaded applications. Singleton patterns are often used as global variables because the global variables permit allocation and initialization whenever required. They don't permit to pollute the global namespace with unnecessary variables.

 

package singleton;
import org.apache.log4j.Priority;
import java.util.GregorianCalendar;
import java.text.SimpleDateFormat;
import java.io.FileOutputStream;
import java.util.Properties;
import java.io.PrintStream;
import java.io.InputStream;
import java.io.IOException;

public class Logger {
private String fileName;
private Properties properties;
private Priority priority;
private Logger() {
logger = this;
}

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;
}

private String
getFileName(GregorianCalendar gc) {
SimpleDateFormat dateFormat1 = new
SimpleDateFormat("dd-MMM-yyyy");
String dateString =
dateFormat1.format(gc.getTime());
String fileName =
"C:\\prashant\\patterns\\log\\
Patterns ExceptionLog-" +
dateString + ".txt";

Sept 2007 | Java Jazz Up | 44
 
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,   Download PDF