dcsimg
Differences between singleton pattern and static classes
4 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Tuesday, July 2, 2002 02:18 AM

I need a class for logging so I decided to develope a singleton patterned one. But I invoke in a static context and I wonder, what's the difference between... public class MyClass { private static Solitario solo = Solitario.getInstance(); //Singleton. I need to be static in order to use in the next method //Util method of MyClass public static synchronized int method(String className) throws Exception { solo.justDoIt(); //I invoke a method of a singleton object that is satic in MyClass }//End method }//End MyClass ... and ... public class MyClass { //Util method of MyClass public static synchronized in   More>>


I need a class for logging so I decided to develope a singleton patterned one. But I invoke in a static context and I wonder, what's the difference between...



			
public class MyClass {
private static Solitario solo = Solitario.getInstance(); //Singleton. I need to be static in order to use in the next method

//Util method of MyClass
public static synchronized int method(String className) throws Exception {
solo.justDoIt(); //I invoke a method of a singleton object that is satic in MyClass
}//End method
}//End MyClass



... and ...



			
public class MyClass {

//Util method of MyClass
public static synchronized int method(String className) throws Exception {
NoSolitario.justDoIt(); //I invoke a static method of a class (not instantiated)
}//End method
}//End MyClass



...?




I'd like to know the differences in general BETWEEN creating a singleton object and calling its methods AND making a class with static methods and calling them without instantiating any object.



Thanks.    <<Less

Re: Differences between singleton pattern and static classes

Posted By:   Anonymous  
Posted On:   Friday, August 30, 2002 01:50 AM

Well I've read all the other answers and I notice that they all miss an important point.

The static class members are created (and destroyed) outside of your control and if that creation is expensive and you never use it then that's not too efficient.

An explicitly coded Singleton (using a Factory as a rule) would allow you to create the Singleton instance as and when needed (ie the first time the factory is called), and destroy it if you decide that's more efficient.

Singleton pattern vs static methods... which is better?

Posted By:   Eric_Lindauer  
Posted On:   Wednesday, August 14, 2002 09:02 PM

These are excellent points about the differences between static methods and singletons. I would go farther though and argue that the advantage of polymorphism in the returned type makes the singleton pattern superior to the static method design.




One of the main goals of OO programming is to make objects and packages which are open to extension while being closed to modification. When you choose to make your objects all static methods, you have effectively cut off any future attempts to extend your objects without modifying your source code. However, with the singleton pattern, it is often a trivial matter to have a dynamic way to determine the type of class to instantiate ( a Properties objects or some other type of runtime preference comes to mind ), and in this way, other users of your code can plugin their own implementation, and all the code you've already written that uses the singleton continues to work, but in the new way that the third-party requires.




This is, in my opinion, one of the fundamental criteria of a good design (closed to mods, open to extension). As such, I'd recommend that you always choose the singleton pattern over the class-full-of-static-methods.




Hope this helps.

Eric

Re: Differences between singleton pattern and static classes

Posted By:   David_Liu  
Posted On:   Monday, August 5, 2002 06:30 AM

Eventhough Andre has a good point, I think the answer is alot simpler then that.


With pattern, you main purpose is to reuse an existing design, and most importantly, effectively communicate your ideas with other developers and architects.


You can probably do just about everything using static class as you can with Singleton. If someone is looking over your design, they might be questioning why you want to put a static class in a particular location. If you using singleton, on the other hands, it sends a clear sign that you want a single entry class.

Re: Differences between singleton pattern and static classes

Posted By:   Anonymous  
Posted On:   Tuesday, July 2, 2002 03:36 AM

With a Singleton pattern you can separate the actual Singleton class and the class that provides the Singleton.
This makes the implementation of the Singleton class replaceable (even at runtime), e.g.



public class MySingletonProvider {
private static MySingleton singletonInstance = null;

public static MySingleton getInstance() {
if(singletonInstance == null) {
singletonInstance = new MyConcreteSingleton1();
// or new MyConcreteSingleton2()
}
}
}

public interface MySingleton
{
void method();
}

public class MyConcreteSingleton1 implements MySingleton
{
public void method() {
// do something
}
}

public class MyConcreteSingleton2 implements MySingleton
{
public void method() {
// do something else
}
}

As you can see, the provider class could choose different implementations of a singleton (e.g. based on informations in a configuration file). You could use this to provide several logging mechanisms, one for logging to a database, another for logging to a file, etc.

About | Sitemap | Contact