[Java] Helper method(헬퍼 메소드)
Wikipedia definition(위키피디아 정의)
: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. An instance of a helper class is called a helper object (for example, in the delegation pattern).
:객체 지향 프로그래밍에서 헬퍼 클래스는 어떤 기능을 제공하기 위해 사용되는 클래스로 어플리케이션이나 클래스의 주 목적으로 사용되는 것이 아니다. 헬퍼 클래스의 인스턴스는 헬퍼 오브젝트다(위임 패턴이 그 예 중 하나다).
아래는 한 블로그에서 참고한 예를 조금 수정했다. main()에서 반복적으로 preMr()이 호출되는 것을 확인할 수 있다. 이 처럼 주요한 목적을 가는 클래스나 메소드에서 반복적으로 호출이 필요한 경우 매번 새로이 정의하지 않고 한 번 선언이 돼 불필요하게 반복되는 코드를 줄여주는 목적으로 사용되는 것이 Helper class와 Helper method이다.
public class HelperMethods {
public static String preMr(String name) {
return "Mr. " + name;
}
public static String preMs(String name) {
return "Ms. " + name;
}
public static String preDr(String name) {
return "Dr. " + name;
}
}
public class Test {
public static void main(String[] args) {
String name1 = "Manav";
System.out.println(HelperMethods.preMr(name)); // Mr. Manav
String name2 = "Daniel";
System.out.println(HelperMethods.preMr(name)); // Mr. Daniel
String name3 = "Calm";
System.out.println(HelperMethods.preMr(name)); // Mr. Calm
}
}
참고
- https://teamtreehouse.com/community/what-exactly-is-a-helper-method
- https://en.wikipedia.org/wiki/Helper_class
Helper class - Wikipedia
en.wikipedia.org
- https://www.youtube.com/watch?v=rXvEx9zPsRg