데코레이터 패턴(Decorator Pattern)
데코레이터 패턴에 대해 알아봅니다.
문제 상황
public class Beverage {
String description = "어떤 커피인지 설명합니다.";
double milkCost = 0.0;
double soyCost = 0.0;
double mochaCost = 0.0;
double whipCost = 0.0;
public double cost() {
double condimentCost = 0.0; // 첨가물의 가격
if (hasMilk()) {
condimentCost += milkCost;
}
if (hasSoy()) {
condimentCost += soyCost;
}
if (hasMocha()) {
condimentCost += mochaCost;
}
if (hasWhip()) {
condimentCost += whipCost;
}
return condimentCost;
}
public boolean hasMilk() {
return milkCost != 0.0;
}
public void setMilk(double milkCost) {
System.out.println("우유 추가");
this.milkCost = .10;
}
...
}
public class DarkRoast extends Beverage {
public DarkRoast() {
description = "다크 로스트 커피";
}
public double cost() {
return 1.99 + super.cost();
}
}해결 방법

Java.io 패키지

Last updated