Difference between singleton class and static class?
Possible Duplicates:
Difference between static class and singleton pattern?
What is the difference between a Singleton pattern and a static class in Java?
HI I am not clearly getting What's the difference between a singleton class and a static class? Can anybody elaborate this with example?
Old que/ans on SO : Difference between static class and singleton pattern?
A static class is one that has only static methods, for which a better word would be "functions". The design style embodied in a static class is purely procedural.
Singleton, on the other hand, is a pattern specific to OO design. It is an instance of an object (with all the possibilities inherent in that, such as polymorphism), with a creation procedure that ensures that there is only ever one instance of that particular role over its entire lifetime.
Singleton Class : Singleton Class is class of which only single instance can exists per classloader.
Static/Helper Class (Class with only static fields/methods) : No instance of this class exists. Only fields and methods can be directly accessed as constants or helper methods.
Following is referenced from this blog " Static classes in Java " describes it nicely. The blog also has examples for explaining same:
Singleton example:
public class ClassicSingleton {
private static ClassicSingleton instance = null;
private ClassicSingleton() {
// Exists only to defeat instantiation.
}
public static ClassicSingleton getInstance() {
if (instance == null) {
instance = new ClassicSingleton();
}
return instance;
}
}
Static Class example:
/**
* A helper class with useful static utility functions.
*/
public final class ActionHelper {
/**
* private constructor to stop people instantiating it.
*/
private ActionHelper() {
// this is never run
}
/**
* prints hello world and then the users name
*
* @param users
* name
*/
public static void printHelloWorld(final String name) {
System.out.println("Hello World its " + name);
}
}
So what's the difference between the two examples and why do I think the second solution is better for a class you don't want or need to instantiate. Firstly the Singleton pattern is very useful if you want to create one instance of a class. For my helper class we don't really want to instantiate any copy's of the class. The reason why you shouldn't use a Singleton class is because for this helper class we don't use any variables. The singleton class would be useful if it contained a set of variables that we wanted only one set of and the methods used those variables but in our helper class we don't use any variables apart from the ones passed in (which we make final). For this reason I don't believe we want a singleton Instance because we do not want any variables and we don't want anyone instantianting this class. So if you don't want anyone instantiating the class, which is normally if you have some kind of helper/utils class then I use the what I call the static class, a class with a private constructor and only consists of Static methods without any any variables.
Same answer is also referenced from my answer here
The difference is not the correct way to ask.because singleton is not a keyword compared to static. you should be asking like "When to choose which one?". what are the advantages of singleton class over static class, these questions comes at the design stages.
Singleton: Usage: classes that serve as global configuration , ex: Trial version of software with one database connection, JDK Runtime classes instances per jvm.
When to go: 1.While developing your code,you think of forward compatibilty, like tomorrow when you need to convert this singleton class to normal class or allow subclassing. 2. You can provide lazy loading feature , when this singleton class is heavy.
static: Usage: classes that basically does conversions,utility functions. please check Math class.
When to go: 1. helper classes, used by all the classes in your api development.
disadvantage: 1. classes are eagerly loaded .
expecting points from other people.
链接地址: http://www.djcxy.com/p/78812.html上一篇: Java单例应该使用静态变量吗?
下一篇: 单例类和静态类之间的区别?