Helper class
![]() | This article does not cite any sources. (February 2020) |
In object-oriented programming, a helper class is used to assist in providing some functionality, which isn't the main goal of the application or class in which it is used[citation needed]. An instance of a helper class is called a helper object (for example, in the delegation pattern).
Helper classes are often created in introductory programming lessons, after the novice programmer has moved beyond creating one or two classes.
A is a special case of a helper class in which the methods are all static. In general, helper classes do not have to have all static methods, but may have instance variables. Multiple instances of the helper class may exist as well.
Example[]
This is also an example of a .
public class PrependHelper
{
// static functions
public static String meowPrepend(String text)
{
return "Meow meow " + text + "!";
}
public static String woofPrepend(String text)
{
return "Woof woof " + text + "!";
}
public static String woohPrepend(String text)
{
return "Wooh " + text + "!";
}
}
Alternative to Helper Class[]
Functions which are going to helper classes could be placed close to where they are used. The other alternative is wrapping helper class parameters into a class as a field. That class can have a name from the business domain connected to the fields it has. The example below shows how to convert helper methods to methods on domain types:
public class Text
{
String text;
public String meowPrepend()
{
return "Meow meow " + text + "!";
}
public String woofPrepend()
{
return "Woof woof " + text + "!";
}
public String woohPrepend()
{
return "Wooh " + text + "!";
}
}
See also[]
- Hierarchy
- Class diagram (UML)
- Class-based programming
- Class (computer programming)