Are utility classes evil? [closed]

Utility classes aren’t exactly evil, but they can violate the principles that compose a good object-oriented design. In a good object-oriented design, most classes should represent a single thing and all of its attributes and operations. If you are operating on a thing, that method should probably be a member of that thing.

However, there are times when you can use utility classes to group a number of methods together — an example being the java.util.Collections class which provides a number of utilities that can be used on any Java Collection. These aren’t specific to one particular type of Collection, but instead implement algorithms that can be used on any Collection.

Really, what you need to do is think about your design and determine where it makes the most sense to put the methods. Usually, it’s as operations inside of a class. However, sometimes, it is indeed as a utility class. When you do use a utility class, however, don’t just throw random methods into it, instead, organize the methods by purpose and functionality.

Leave a Comment