Merged
Conversation
guswns3371
commented
Nov 13, 2022
Comment on lines
+6
to
+25
| /** | ||
| * 첨가물 | ||
| */ | ||
| 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(); | ||
| } | ||
| } |
Member
Author
There was a problem hiding this comment.
데코레이터 클래스의 형식은 해당 클래스가 감싸는 클래스의 형식과 동일하다.
형식을 동일하게 하기 위해 "상속"이나 "인터페이스 구현"을 활용할 수 있다.
헤드퍼스트 디자인패턴 예시에는 "상속"으로 형식을 동일하게 하였다.
Comment on lines
+7
to
+36
| 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(); | ||
| } |
Member
Author
There was a problem hiding this comment.
Beverage를 추상 클래스 또는 인터페이스로 정의하여 데코레이터 패턴을 활용할 수 있음
Comment on lines
+6
to
+24
| 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() + ", 모카"; | ||
| } | ||
| } |
Member
Author
There was a problem hiding this comment.
구상 클래스는 구상 요소에 맞게 새로운 기능/데이터를 더하여 행동을 확장한다.
cost():return beverage.cost() + 0.35;(0.35 <- 새로운 데이터)getDescription():return beverage.getDescription() + ", 모카";(모카 <- 새로운 데이터)
Comment on lines
+18
to
+37
| 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); // 두유 + 모카샷 + 휘핑크림 추가한 블랜드 커피 | ||
| } |
Member
Author
There was a problem hiding this comment.
객체를 여러 번 감싸므로써 행동을 위임하여 새로운 기능/데이터를 더한다.
- 지금은 new 키워드로 객체를 생성하지만, 추후에 팩토리 패턴/빌더 패턴과 같이 더 나은 방법으로 객체를 생성할 수 있다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.