tags:

views:

57

answers:

5

I am looking at a codebase and I often see something like:

public class SomeClass
{
 protected static SomeClass myObject;

 //...

 public static SomeClass getObject()
 {
  return myOjbect
 }
}

I'd like to make sure I understand the purpose behind this. Is it to ensure one instance of the class gets shared even if it is instantiated multiple times? I am not sure about the vocabulary here, or else I'd search for the answer, so if this pattern has a name, please let me know.

Also, this seems a little chicken-and-egg definition because the class includes an object of the type of the class. Why isn't this actually paradoxical?

Thanks!

+3  A: 

The pattern you mentioned is called "Singleton", but from your code sample it is not clear if this is really what is intended. Due to the fact that the member is protected, I would guess not - if there are subclasses, then there would probably not be a single instance.

Thomas Lötzer
+1  A: 

It's not all that uncommon; it can be a good way to implement the Singleton pattern. There can be other uses as well - sometimes you will want a handful - and no more - of objects of a given class; that class is a good place to hang onto them. In the event that you don't want other classes to be able to create objects of this class, it is common to give the class a private constructor as well.

It's not paradoxical, because the compiler can be aware of a reference to the class before it has fully compiled the class. Later - if you like to think of it this way - it can "fill in the blanks".

Carl Manaster
+4  A: 

This is called the Singleton design pattern.

You are correct in stating that the purpose is to ensure only one instance of the class gets created.

Wikipedia has a preyty good article on the pattern.

jjnguy
+1  A: 

It's called Singleton. You ensure the creation of just ONE (1) object of a given class.

You should add a private Constructor, so the only one who create the object is the class.

public class SomeClass
{
 // Using private constructor
 protected static SomeClass myObject = new SomeClass();

 private SomeClass(){
 //...
 }

 public static SomeClass getObject()
 {
  return myOjbect
 }
}

Much much more here, in Wikipedia

You may want to take a look to Factory Pattern

santiagobasulto
+1  A: 

This is really only common with the Singleton Pattern where there is only this one instance of the class. While it has its uses, Singleton is over- and misused more often than not (usually to disguise procedural programming as OO). It also occurs very often in example code for Java AWT or Swing, where you typically subclass Frame / JFrame, and create an instance in a main method inside the same class.

Also, this seems a little chicken-and-egg definition because the class includes an object of the type of the class. Why isn't this actually paradoxical?

Why do you think it is? The class mainly describes what members instances of this type have - but a static member does not belong to an instance, it belongs to the class itself, so it doesn't have anything to do with the "blueprint" role of the class. Static members are really somewhat un-OO because of that.

But even on the instance level you can have references of the same type. For example, an entry in a linked list would typically have two references to the next and previous entries, which are of the same class.

Michael Borgwardt