https://github.com/Koojiny/Inflearn-warming-up-club-2
public class Order {
private final List<Item> items;
private final String customerInformation;
private final Logger log = Logger.getLogger(this.getClass().getName());
public Order(List<Item> items, String customerInformation) {
this.items = items;
this.customerInformation = customerInformation;
}
public boolean validateOrder() {
try {
checkOrderStatus();
return true;
} catch (OrderException e) {
log.info(e.getMessage());
} catch (Exception e) {
log.info("프로그램에 문제가 발생했습니다.");
}
return false;
}
private void checkOrderStatus() throws OrderException {
// 1. 주문항목 존재 여부 확인
validateItem();
// 2. 총 가격 유효성 확인
validatePrice();
// 3. 사용자 정보 유효성 확인
validateCustomerInformation();
}
private void validateItem() throws OrderException {
if (hasNotItem()) {
throw new OrderException("주문 항목이 없습니다.");
}
}
private void validatePrice() throws OrderException {
if (isInvalidPrice()) {
throw new OrderException("올바르지 않은 총 가격입니다.");
}
}
private void validateCustomerInformation() throws OrderException {
if (hasCustomerInfo()) {
throw new OrderException("사용자 정보가 없습니다.");
}
}
private boolean hasNotItem() {
return this.items.isEmpty();
}
private boolean isInvalidPrice() {
return calculateTotalPrice() <= 0;
}
private int calculateTotalPrice() {
int sum = 0;
for (Item item : this.items) {
int price = item.getPrice();
sum += price;
}
return sum;
}
private boolean hasCustomerInfo() {
return this.customerInformation.isEmpty();
}
}
단 한 가지의 변경 이유
**만을 가져야 한다예시) 스마트폰 앱과 알림
- 스마트폰에서 카메라 앱은 사진 촬영만, 전화 앱은 전화만 담당한다
- 카메라 앱에서 전화, 문자, 알림 등의 기능이 있다면 유지보수가 어렵다
예시) 자동차의 내비게이션 시스템
- 기존 내비게이션에 새로운 지도 업데이트를 할 때, 자동차를 바꾸거나 부품을 바꾸는 것이 아닌 시스템 업데이트만으로 지도 정보 확장이 가능하다
- 시스템 확장에는 열려 있지만 기존 구조는 바뀌지 않는다