In
object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impl ...
, 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. An instance of a helper class is called a helper object (for example, in the
delegation pattern
In software engineering, the delegation pattern is an object-oriented design pattern that allows object composition to achieve the same code reuse as inheritance.
In delegation, an object handles a request by delegating to a second object (the ...
).
Helper classes are often created in introductory programming lessons, after the novice programmer has moved beyond creating one or two classes.
A
utility class 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
utility class. Below are
extension method
In object-oriented computer programming, an extension method is a method added to an object after the original object was compiled. The modified object is often a class, a prototype, or a type. Extension methods are features of some object-o ...
s on the string object.
public static class PrependHelper
Alternative to helper class
Functions which are going to be 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
References
See also
*
Hierarchy
A hierarchy (from Ancient Greek, Greek: , from , 'president of sacred rites') is an arrangement of items (objects, names, values, categories, etc.) that are represented as being "above", "below", or "at the same level as" one another. Hierarchy ...
*
Class diagram
In software engineering,
a class diagram
in the Unified Modeling Language (UML) is a type of static structure diagram that describes the structure of a system by showing the system's classes, their attributes, operations (or methods), and the re ...
(UML)
*
Class-based programming
Class-based programming, or more commonly class-orientation, is a style of object-oriented programming (OOP) in which inheritance (object-oriented programming), inheritance occurs via defining ''class (computer programming), classes'' of object ( ...
{{Types of programming languages
Class (computer programming)