A Techno Blog, mainly about Java

Singleton Pitfalls

The singleton pattern [uml] [GOF reference]

Simply Singleton: The Singleton pattern is one of the simplest design patterns, but for Java developers, it’s full of pitfalls

The Single Java Object  – A singleton is an class that can be instantiated once, and only once. This is a fairly unique property, but useful in a wide range of object designs.

The double-checked locking idiom – be used with the Singleton creation pattern to limit how much code is synchronized. However, it is not guaranteed to work. Instead of failing consistently, it will fail sporadically.

When to use singletons:

  • Will every application use this class exactly the same way? (exactly is the key word)
  • Will every application ever need only one instance of this class? (ever and one are the key words)
  • Should the clients of this class be unaware of the application they are part of?

Lazy Instantiation


June 2, 2005 - Posted by | Patterns & UML

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: