-
Notifications
You must be signed in to change notification settings - Fork 0
3주차 데코레이터 패턴 #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
3주차 데코레이터 패턴 #3
Conversation
| /** | ||
| * 첨가물 | ||
| */ | ||
| public abstract class CondimentDecorator extends Beverage { | ||
| /** | ||
| * 각 데코레이터가 감쌀 음료를 나타내는 Beverage 객체를 저장할 인스턴스 변수. | ||
| * 모든 Beverage의 구상 클래스를 담기 위해 "Beverage"라는 슈퍼클래스 유형을 사용한다. | ||
| * <p> | ||
| * Wrapping 하여 데코레이팅한다. | ||
| */ | ||
| protected Beverage beverage; | ||
|
|
||
| @Override | ||
| public abstract String getDescription(); | ||
|
|
||
| @Override | ||
| public Size getSize() { | ||
| return beverage.getSize(); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
데코레이터 클래스의 형식은 해당 클래스가 감싸는 클래스의 형식과 동일하다.
형식을 동일하게 하기 위해 "상속"이나 "인터페이스 구현"을 활용할 수 있다.
헤드퍼스트 디자인패턴 예시에는 "상속"으로 형식을 동일하게 하였다.
| public abstract class Beverage { | ||
|
|
||
| protected static final EnumMap<Size, Double> sizeCostMap; | ||
|
|
||
| static { | ||
| sizeCostMap = new EnumMap<>(Size.class); | ||
| sizeCostMap.put(Size.TALL, 1.0); | ||
| sizeCostMap.put(Size.GRANDE, 1.2); | ||
| sizeCostMap.put(Size.VENTI, 1.5); | ||
| } | ||
|
|
||
| protected Size size = Size.TALL; | ||
| protected String description = "제목 없음"; | ||
|
|
||
| // 추상 클래스에서 getDescription() 을 미리 구현됨. | ||
| public String getDescription() { | ||
| return description; | ||
| } | ||
|
|
||
| public Size getSize() { | ||
| return size; | ||
| } | ||
|
|
||
| public void setSize(Size size) { | ||
| this.size = size; | ||
| } | ||
|
|
||
| // cost() 는 서브 클래스에서 구현해야 함. | ||
| public abstract double cost(); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Beverage를 추상 클래스 또는 인터페이스로 정의하여 데코레이터 패턴을 활용할 수 있음
| public class Mocha extends CondimentDecorator { | ||
| public Mocha(Beverage beverage) { | ||
| this.beverage = beverage; | ||
| } | ||
|
|
||
| @Override | ||
| public double cost() { | ||
| // 장식하고 있는 객체(beverage)에 cost() 작업을 위임하여 리턴값을 구한뒤 | ||
| // 그 결과에 모카 가격을 사이즈게 맞게 추가로 계산 | ||
| return beverage.cost() + 0.35; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| // 장식하고 있는 객체(beverage)에 대한 설명에 | ||
| // 그 결과에 모카에 대한 설명을 추가로 붙힘 | ||
| return beverage.getDescription() + ", 모카"; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
구상 클래스는 구상 요소에 맞게 새로운 기능/데이터를 더하여 행동을 확장한다.
cost():return beverage.cost() + 0.35;(0.35 <- 새로운 데이터)getDescription():return beverage.getDescription() + ", 모카";(모카 <- 새로운 데이터)
| private static void decorating() { | ||
| final Beverage espresso = new Espresso(); | ||
| printBeverage(espresso); | ||
|
|
||
| Beverage darkRoast = new DarkRoast(); | ||
| printBeverage(darkRoast); // 일반 다크 로스트 커피 | ||
|
|
||
| darkRoast = new Mocha(darkRoast); // mocha로 감싸기 | ||
| darkRoast = new Mocha(darkRoast); // mocha로 한 번 더 감싸기 | ||
| darkRoast = new Whip(darkRoast); // 휘핑크림 추가 | ||
| printBeverage(darkRoast); // 모카샷2 + 휘핑크림 추가한 다크 로스트 커피 | ||
|
|
||
| Beverage houseBlend = new HouseBlend(); | ||
| printBeverage(houseBlend); // 일반 하우스 블랜드 커피 | ||
|
|
||
| houseBlend = new Soy(houseBlend); | ||
| houseBlend = new Mocha(houseBlend); | ||
| houseBlend = new Whip(houseBlend); | ||
| printBeverage(houseBlend); // 두유 + 모카샷 + 휘핑크림 추가한 블랜드 커피 | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
객체를 여러 번 감싸므로써 행동을 위임하여 새로운 기능/데이터를 더한다.
- 지금은 new 키워드로 객체를 생성하지만, 추후에 팩토리 패턴/빌더 패턴과 같이 더 나은 방법으로 객체를 생성할 수 있다.
No description provided.