요약
그렇다.
- getter도 메소드이기 때문이다
- 응용하면 getter 메소드는 유지하면서 구현 클래스에서 멤버 값을 그대로 보내는 대신, 기존의 멤버로 계산된 값을 리턴하게 구현할 수 있다.
사용 사례
Java List의 `.size()`
주의점 (의견)
그래도 인터페이스는 공유해야할 행동을 정의하는 것이 더 좋기 때문에 interface에 멤버 값을 반환하는 getter를 남용하는 것은 지양하는 게 좋을 것 같다. 정말 그 기능을 구현하는 클래스가 공통적으로 특정 값을 반환할 필요가 있을 때만 선언하는 것이 좋을 것 같다. callee가 호출할 수 있도록.
사실 찾아본 목적은 DTO에서 특정 필드를 재사용하기 위해서였다. 이 경우, Spring framework은 사실 getter와 setter라는 public 메소드에만 의존하니 추가해도 좋을 것 같다.
Java Interface Usage Guidelines -- Are getters and setters in an interface bad?
What do people think of the best guidelines to use in an interface? What should and shouldn't go into an interface? I've heard people say that, as a general rule, an interface must only define be...
stackoverflow.com
Use getter interfaces as a way to reuse DTO fields
Assume I have a lot of DTO for a same domain model like this: class User{} //DTO @Value.Immutable interface SimpleUserDTO { getName getAge } @Value.Immutable interface RegisterUser...
softwareengineering.stackexchange.com
'Computer Science > Object Oriented Programming' 카테고리의 다른 글
[OOP] 순환 참조 (Circular reference) - 문제점, 해결법 (설계 관점) (0) | 2024.08.30 |
---|---|
[OOP] Factory (0) | 2024.04.09 |