<Chapter 9>

명령-질의 분리 원칙 (Command-Query Separation principle, CQS)

에서 명령과 질의를 분리 하라에서

public int getScore() {
      int score = 0;
      for (Criterion criterion: criteria) 
         if (criterion.matches(answerMatching(criterion))) 
            score += criterion.getWeight().getValue();
      return score;
   }

위와 같은 코드는 괜찮은가?

GET하는데 Criterion을 돌면서 값을 계산해서 반환한다.

여기서 명령이란 정확히 뭘까

어떤 상태 값을 바꾸는 것?

쓰기 작업?

시스템의 상태를 변경하는 것이라고 한다.

시스템에 있는 어떤 클래스 혹은 엔티티의 상태 변경 하는 것을 말한다.

CQRS : https://coding-start.tistory.com/265


<Chapter 10> 목 객체 사용

public class AddressRetriever {
   public Address retrieve(double latitude, double longitude)
         throws IOException, ParseException {
      String parms = String.format("lat=%.6flon=%.6f", latitude, longitude);
      String response = new HttpImpl().get(
        "<http://open.mapquestapi.com/nominatim/v1/reverse?format=json&>"
        + parms);

      JSONObject obj = (JSONObject)new JSONParser().parse(response);

      JSONObject address = (JSONObject)obj.get("address");
      String country = (String)address.get("country_code");
      if (!country.equals("us"))
         throw new UnsupportedOperationException(
            "cannot support non-US addresses at this time");

      String houseNumber = (String)address.get("house_number");
      String road = (String)address.get("road");
      String city = (String)address.get("city");
      String state = (String)address.get("state");
      String zip = (String)address.get("postcode");
      return new Address(houseNumber, road, city, state, zip);
   }
}