Tuesday, 30 July 2013

Mis-Using Abstract for Static Classes

Recently came across this construction in the code we have at work.


Now, some notes:

  • nowhere was a subclass defined to implement the 'abstract methods' in StringUtilities.
  • as a matter of fact, there were no 'abstract methods'.
  • all other methods were defined 'static'

It seemed a weird way to defeat instantiation (badly, by the way, as a person could simply subclass the thing and instantiate that).

Proper defeating of instantiation is done by means of a private constructor.


But, if what you need is a Singleton, perhaps it's better to just define a single ENUM.

Nobody could quite explain to me in which cases the first example would be a "Good Idea"™.

No comments:

Post a Comment