public class OrderContext {
// 오전반 설정: 사람 바리스타(창고주입) + 캐셔
public static Cashier configMorningShift() {
// 1. 창고를 먼저 만들고
BeanStorage storage = new BeanStorage();
// 2. 창고를 가진 바리스타를 만들고
Barista barista = new Barista(storage);
// 3. 그 바리스타와 일할 캐셔를 만듭니다.
return new Cashier(barista);
}
// 오후반 설정: 로봇 바리스타 + 캐셔
public static Cashier configAfternoonShift() {
return new Cashier(new RobotBarista()); // 의존성 주입 (DI)
}
}
이렇게 하면 캐셔와 바리스타 객체는 자신이 할 일만 알면 된다.
지금까지는 객체 생성을 main에 전부 맡겼는데 이걸 안 이후부터는 객체 생성을 컨텍스트에 맡겨, 더 객체지향적인 코드를 작성할 수 있을 것이다.
public enum Menu {
AMERICANO(20, "아메리카노"),
LATTE(30, "카페라떼"),
ESPRESSO(10, "에스프레소");
private final int requiredBeans;
private final String description;
Menu(int requiredBeans, String description) {
this.requiredBeans = requiredBeans;
this.description = description;
}
public int getRequiredBeans() { return requiredBeans; }
public String getDescription() {
return description;
}
}
각각의 menu에 원두 소비량과 메뉴 이름을 mapping한다.
그리고 private final 로 선언하여 불변의 값으로 만든다.
그리고 각 값을 가져올 수 있는 getter를 만든다.
이러면 나중에 메뉴가 늘어나도 enum만 수정하면 되므로 편해진다.