Monday, July 21, 2008

Breaking the Singleton Pattern



Reactions: 

I like design patterns. They provide intuitive and reusable solutions to many software design problems. Singleton is the most basic of them. I use it everywhere from creating a Library class which initialize series of application wide objects to threaded queues used by various components within a system to lazy write the data.

In a nutshell Singleton guarantees only one instance of a class. Simple enough. See the following code.



package home;

public class AlwaysSingle
{
private String _name = null;
private static AlwaysSingle _single = null;

private AlwaysSingle( String name )
{
_name = name;
}

public static AlwaysSingle getSingle( String name )
{
if( _single == null )
{
_single = new AlwaysSingle( name );
}
return _single;
}

public String getName()
{
return _name;
}
}


Hmm, can somebody tell me how can I have code coloring in Blogger?
[After writing this article I searched for online code beautifiers. Most of the tools do not perform code coloring. They are mere formatters and others are standalone! I will write one in free time.]

So, this code has private constructor and have a static instance which can only be reached by calling single entry point. Nothing can be done to create more instances. Class is final, so one can't do inheriting tricks.

Reflection. Ring any bells? I still have to see writing security contexts as part of standard development practices. Look at the following code snippet. Pretty straightforward. Here goes Singleton.


import home.AlwaysSingle;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Driver
{
public static void main(String[] args)
throws IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException
{
AlwaysSingle single = AlwaysSingle.getSingle("me");

System.out.println(single.getName());

Class klass = Class.forName("home.AlwaysSingle");

Class[] paramTypes = {
String.class,
};

Constructor one = klass.getDeclaredConstructor(paramTypes);
one.setAccessible(true);

Object[] ags = {
"you",
};

AlwaysSingle notanymore = (AlwaysSingle) one.newInstance(ags);

System.out.println(notanymore.getName());
}
}


I can automatically retrieve the knowledge I have regarding this class. Sorry for not discussing this code. I am out of time and lazy for that :). I am assuming anyone who cared to read after reflection will be able to figure out things.

8 comments:

Sebastiaan Deckers said...

Hey buddy, congrats on the new blog!

Here is a code beautifier that works on Blogger:
http://blog.gpowered.net/2007/07/howto-post-code.html

Mohit Aggarwal said...

Wow!! Thanks a ton. This will surely be of great help.

Anonymous said...

Hello. And Bye.

Anonymous said...

The French gourmet cheese Bleu d'Auvergne has a wonderful aroma, a rich taste; the saltiness increases with the incidence of veining. The overall flavor is piquant but not overly sharp. Bleu d'Auvergne started life as an imitation of Roquefort, using cow's milk in place of sheep's milk. Legend has it that a peasant, around 1845, decided to inject his cheese with a blue mold that he found growing on his left-over bread (the motto being, waste not, want not). And thus, the gourmet cheese Bleu d'Auvergne was born. This French gourmet blue cheese comes from the region of Auvergne and the cheese is made from milk of Salers and Aubrac cows. The rind is very thin and so the cheese is usually wrapped in foil. The cheese is rich and creamy with a pale yellow color and scattered holes and well-defined greenish-blue veining. We cut and wrap this cheese in wedge of 8 ounces and 1 pound.

buy fresh blue cheese

[url=http://riderx.info/members/buy_5F00_fresh_5F00_blue_5F00_cheese.aspx]buy fresh blue cheese[/url]

http://riderx.info/members/buy_5F00_fresh_5F00_blue_5F00_cheese.aspx

Anonymous said...

Hello,
I have developed a new clean web 2.0 wordpress theme.

Has 2 colours silver and blue, has custom header(colour or image).
I am curently working on it, so if you have suggestions let me know.

You can view live demo and download from here www.getbelle.com
If you found bug reports or you have suggestions pm me.
Wish you a happing using.

many thanks to [url=http://www.usainstantpayday.com/]USAInstantPayDay.com[/url] for paying the hosting and developement of the theme

mayommats

Anonymous said...

It's so easy to choose high quality [url=http://www.euroreplicawatches.com/]replica watches[/url] online: [url=http://www.euroreplicawatches.com/mens-swiss-watches-rolex/]Rolex replica[/url], [url=http://www.euroreplicawatches.com/mens-swiss-watches-breitling/]Breitling replica[/url], Chanel replica or any other watch from the widest variety of models and brands.

Anonymous said...

Hi there everybody, I just registered on this terrific forum and wished to say what's up! Have a wonderful day!

Anonymous said...

megan fox transformers 2 photos, [url=http://discuss.tigweb.org/thread/187756]megan fox bmp[/url] boobs megan fox
kim kardashian butts, [url=http://discuss.tigweb.org/thread/187768]kim kardashian makeup[/url] download kim kardashian superstar
taylor swift tickets+moline, il, [url=http://discuss.tigweb.org/thread/187772]taylor swift with fans[/url] pics of taylor swift
how can i talk to hannah montana, [url=http://discuss.tigweb.org/thread/187786]when is the next hannah montana concert[/url] fake pictures of hannah montana naked
quotes from harry potter, [url=http://discuss.tigweb.org/thread/187792]harry potter and the half blood prince blu-ray interview[/url] harry potter and the deathly hallows summary
baltimore cruise port places close by to shop, [url=http://discuss.tigweb.org/thread/187798]threats to carnival cruise lines[/url] what cruise line sails from new orleans to the panama canal
full images of justin bieber, [url=http://discuss.tigweb.org/thread/187812]chat with justin bieber to night right now[/url] free justin bieber desktop wallpapers
johnny depp britney spears, [url=http://discuss.tigweb.org/thread/187814]circus britney spears lyrics[/url] britney spears clothing
megan fox leaded sex tape, [url=http://discuss.tigweb.org/thread/175542]megan fox body[/url] megan fox in undies