Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Singleton and Static Utility classes [closed]

What factors influence the appropriate design pattern to use?

Clarification:

The reason I ask this question is because I'm designing an application that requires multiple static factory classes and singleton manager classes. At times, I become confused as to which design I should employ and I thought asking this community why and when may help clarify things for me a bit.

like image 286
mre Avatar asked Apr 07 '11 14:04

mre


People also ask

Can a utility class be singleton?

So yes, basically use static utility methods when you are damn sure (e.g. Math ) you'd never need an instance and use singletons when you think that a single instance is good enough for the time being but might change in the future (e.g. connection providers). Show activity on this post.

What is difference between singleton class and static class?

While a static class allows only static methods and and you cannot pass static class as parameter. A Singleton can implement interfaces, inherit from other classes and allow inheritance. While a static class cannot inherit their instance members. So Singleton is more flexible than static classes and can maintain state.

What is difference between singleton and static class in java?

Singleton is a design pattern that assures a single instance of a Class for the lifetime of an application. It also provides a global point of access to that instance. static – a reserved keyword – is a modifier that makes instance variables as class variables.

What is the difference between singleton and static class in SAP ABAP?

The static methods (declared using CLASS-METHODS) of a class cannot be redefined in subclasses. A singleton is a design pattern where the class has the task of creating objects. The class ensures that only one object exists for every internal session that is made available to consumers.


2 Answers

Singleton is used when a single object needs to be instantiated and all requested object access goes through this particular instance. This object can maintain state if desired.

Static Utility 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.

like image 124
roberttdev Avatar answered Oct 18 '22 23:10

roberttdev


I use static utility classes for shared functions that will be called from many different contexts - e.g. maths functions similar to those in java.util.Math. This is an appropriate pattern assuming that these are "pure" functions (i.e. don't manipulate any state or access any data other than than the parameters they are given).

I very rarely use singletons, and in particular try to avoid global singletons. They suffer from all the usual problems associated with global variables. They make testing difficult, and unless your singleton is also immutable they introduce problems of global state. The main place I have found them useful is in performance hacks that depend on object identity - for example:

  public static final END_OF_SEQUENCE_MARKER=new EndMarker();

Then when traversing a sequence you can just test if (object==END_OF_SEQUENCE_MARKER). Because it's a static final reference, the JIT will turn this into an extremely fast test....

EDIT

Having just seen your clarification, some quick extra comments:

  • Static factory classes don't usually make sense. The whole point of a factory class is that you can instantiate it (or a subclass!), make some configuration changes on the factory object, then use it to generate object instances according to the configuration that you need. If you're going to make it static, you might as well just create a static MyObject.create(..) method rather than having a whole static MyObjectFactory class....
  • Likewise, why have a separate singleton manager class? Usually the best class to manage the singleton is the singleton class itself, since you will typically need it to access a private constructor, assuming you want to guarantee that only one instance will ever be created. Just having a simple static MySingleton.getInstance() method will usually do everything that you need.
like image 30
mikera Avatar answered Oct 18 '22 22:10

mikera