Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In most programming languages classes elude a lot of the type system. While a class, with its static methods and variables is an object, it very often cannot implement an interface or extend other classes. For that reason, it cannot be used in a polymorphic manner, since it cannot be the subtype of another type. For example, if you have an interface <code>IFooable</code>, that is required by several method signatures of other classes, the class object <code>StaticFoo</code> cannot be used in place of <code>IFooable</code>, whereas <code>FooSingleton.getInstance()</code> can (assuming, <code>FooSingleton</code> implements <code>IFooable</code>).</p> <p>Please note, that, as I commented on Heinzi's answer, a singleton is a pattern to control instantiation. It replaces <code>new Class()</code> with <code>Class.getInstance()</code>, which gives the author of <code>Class</code> more control over instances, which he can use to prevent the creation of unneccessary instances. The singleton is just a very special case of the factory pattern and should be treated as such. Common use makes it rather the special case of global registries, which often ends up bad, because global registries should not be used just willy-nilly.</p> <p>If you plan to provide global helper functions, then static methods will work just fine. The class will not act as class, but rather just as a namespace. I suggest, you preserve high cohesion, or you might end up with weirdest coupling issues.</p> <p>greetz<br> back2dos</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload