Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Design Pattern (JAVA) : Singleton Pattern

Design patterns summarize proven solutions for typical object oriented programming problems and help to minimize the design effort and eliminate the common mistakes. To understand the necessity of design patterns, we can always look at the problems and begin by tackling it with alternative or brutal force methods, and learn to appreciate the beauty and implications of design patterns.

In many cases, we wish to maintain a single instance of certain class at run time, e.g., we may wish to keep one single connection to a specific database, or we wish to a have a single sequential number generator to avoid duplication.

We can approach the problem in a few ways, and let's analyze them one by one:
a) We can create a 'global' instance of the class, and let all clients use this instance for activities associated with the class. This is fine provided that every client knows about such arrangement and follow it rigidly and diligently. In other words, we are delegating part of the class design responsibility to the 'customers' of the class. This is a sub-optimal solution.

b) We can monitor and control the class instantiation inside the class definition; we could create a static class member which might be boolean or int, and let the class constructor check it before class creation; no class will be created if the check fails. Yes, this sounds logical and feasible. The next thing we need to consider is: what to do if the check fails? How does the client know about the class instantiation failure? Remember that constructor method does NOT return anything as normal method does. A simple solution would be to let the constructor method throw out an exception, and let the client check and handle the exceptions.

Okay, this looks better, but still a little bit troublesome, right?

c) Using static methods.
class PrintSpooler
{
static String str;
//a static class implementation of Singleton pattern
static public void set(String s)
{
str = s;
}

static public void print()
{
System.out.println(str);
}
}
//==============================
public class staticPrint
{
public static void main(String argv[])
{
PrintSpooler ps = new PrintSpooler();
ps.set("orginal?");

PrintSpooler ps2 = new PrintSpooler();
ps2.set("different?");
ps.print();
}
}

d) How about we make the constructor private, and force user to create an instance of the class using another normal member method? Yes, that is the deal.

class iSpooler
{

static boolean instance_flag = false; //true if 1 instance

private iSpooler() { }

//static Instance method returns one instance or null
static public iSpooler Instance()
{
if (! instance_flag)
{
instance_flag = true;
return new iSpooler(); //only callable from within
}
else
return null; //return no further instances
}

public void finalize()
{
instance_flag = false;
}
}



This post first appeared on Always Remember, You Are At Most Yourself; And, Yo, please read the originial post: here

Share the post

Design Pattern (JAVA) : Singleton Pattern

×

Subscribe to Always Remember, You Are At Most Yourself; And, Yo

Get updates delivered right to your inbox!

Thank you for your subscription

×