Reputation: 33
I heard that singleton is useful and popular ,but the fact is that I seldom see it appears in open source projects.
Instead,I always see people to use static method.
The following one is an example link
public final class IoUtils {
private IoUtils() {
}
public static boolean copyStream(InputStream is, OutputStream os, CopyListener
......
}
}
Singleton Pattern says that just"define a class that has only one instance and provides a global point of access to it".
I think Utils class is suitable to use Singleton because all other classes share to use it.
But Why people nerve use it ?
Upvotes: 1
Views: 57
Reputation: 1771
Singleton design pattern is used when a single object is required to instantiate and all requested object access goes through this particular instance. This object can maintain state if desired. This type of object is required in logger, printing, etc. functionality because they need to maintain state.
Static Utility class is used when you have a class that is just stateless utility functions. It does not maintain state. An instance of the object is never instantiated.
Upvotes: 1