views:

153

answers:

2

I understand that a class that consists of nothing but public static member functions is called a Monotype. And certain classes, such as the Integer class in Java/C#, have various static functions implemented. I am also guide of writing kitchen sink utility classes, like InputCleaner.StripHTMLTags() and etc.

Is there a guideline for Monotypes and public static functions?

For example, let say I have a dice class

class Dice
{

     public static function RollD100()
     {
         ....
     }
}

Should be a Monotype, or a class that has to be instantiated?

I do find a few advantages for classes that have to be instantiated:

  1. You need an object if you have states or data within the class to keep track of
  2. You can employ polymorphism, as there don't seem to be static member functions polymorphism (at least in C++, if I remember correctly. Think PHP 5.3 might have that)

Whereas for a monotype, you get

  1. It functions something like a global function
  2. No steps required for initialization

Other than those, what other guidelines are there?

A: 

Monotypes, singletons and static classes are perfectly valid for some scenarios. But they risk setting a pre-OO mindset, thus robbing you of better code clarity, discoverability and reuse. For example, would something like StripHTMLTags() make more sense as an extension to the String class? (or perhaps to the input widget class)?

Oren Trutner
Personally, I'm against using extension methods like that for all but the most fundamental string operations. Just doesn't seem right to put a ton of methods there that might not be appropriate for some strings. Also, it's less apparent where that method might be implemented.
Thorarin
A: 

The only purpose I have used these for is to collect related but stateless functionality in a single module. As soon as you add state (member variables) you have a Singleton.