남들이 변수에 의존하지 않게 만들고 싶어 변수를 private로 정의한다 그렇다면 어째서 수많은 프로그래머가 get()과 set()을 당연하게 공개해 private 변수를 외부로 노출하는가?
// 목록 6-1 구체적인 Point 클래스 (구현을 외부에 노출)
public class Point {
public double x;
public double y;
}
// 목록 6-2 추상적인 Point 클래스 (구현을 완전히 숨김)
public interface Point {
double getX();
double getY();
void setCartesian(double x, double y);
double getR();
double getTheta();
void setPolar(double r, double theta);
}
// 디미터 법칙 위반
@Service
public class NotificationServiceImpl implements NotificationService {
@Override
publid void sendMessageForSeoulUser(final User user) {
if ("서울".equals(user.getAddress().getRegion())) {
sendNotification(user);
}
}
}
// 디미터 법칙 준수
@Getter
public class User {
private String email;
private String name;
private Address address;
public boolean isSeoulUser() {
return address.isSeoulRegion();
}
}
@Getter
public class Address {
private String region;
private String details;
// 데이터로 사용자의 지역을 파악하는게 아닌 메세지를 통해 파악
public boolean isSeoulRegion() {
return "서울".equals(region);
}
}
final String outputDir = ctxt.getOptions().getScratchDir().getAbsolutePath();
Options opts = ctxt.getOptions();
File scratchDir = opts.getScratchDir();
final String outputDir = scratchDir.getAbsolutePath();
final String outputDir = ctxt.options.scratchDir.absolutePath;