For a utility class, which does not require instantiation and only has static methods, use a private constructor. The compiler can then enforce that no instances are created.
Using all static methods in a utility class supports and enforces the class to have no state. Stateless objects are more efficient and easier to debug and test. All utility classes are stateless. Good candidates for utility classes are where there is a grouping of
I wouldn't call this a pattern so much as a workaround for the deficiencies of languages (such as
) that don't have proper namespaces. Except in such languages, there is no reason to make a class that will never be instantiated, and so this should generally be considered an
, for example, you'd use a module instead of a class for this purpose. In
you'd be better off using a namespace, but my C++ is rusty enough that I hesitate to say for sure.) --
Your C++ isn't rusty.
View edit of
February 9, 2011
with title or text search