1

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 ?

1
  • I don't think implementing behavior in a static class/method is desirable by any OOP guy anymore. Because it is not testable. The best way to have a static class just for ease of use, it is better to have a underlying class that implements the class interface, and re-direct calls to the static methods to corresponding method of the underlying non-static class. The non-static class can be singleton, or the static class can have private member of non-static class type Commented Nov 18, 2017 at 5:26

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.