반응형
public class Main {
public static void main(String[] args) {
Beverage espresso = new Espresso();
espresso = new Milk(espresso);
espresso = new Moka(espresso);
System.out.println(espresso.cost());
System.out.println(espresso.getDescription());
}
}
public abstract class Beverage {
protected String description = "empty";
public String getDescription() {
return description;
}
public abstract int cost();
}
public class Espresso extends Beverage {
public Espresso() {
description = "Espresso";
}
public int cost() {
return 10;
}
}
public abstract class CondimentDecorator extends Beverage {
Beverage beverage;
public abstract String getDescription();
}
public class Milk extends CondimentDecorator {
public Milk(Beverage beverage) {
this.beverage = beverage;
}
@Override
public String getDescription() {
return beverage.getDescription() + " + Milk";
}
@Override
public int cost() {
return beverage.cost() + 5;
}
}
public class Moka extends CondimentDecorator {
public Moka(Beverage beverage) {
this.beverage = beverage;
}
@Override
public String getDescription() {
return beverage.getDescription() + " + Moka";
}
@Override
public int cost() {
return beverage.cost() + 3;
}
}
18
Espresso + Milk + Moka
https://jusungpark.tistory.com/9?category=630296
반응형
'Design Architecture > Design Pattern 예제' 카테고리의 다른 글
Visitor 패턴 예제 (0) | 2022.03.25 |
---|---|
Composite 패턴 예제 (0) | 2021.09.28 |
Template Method 예제 (0) | 2021.09.28 |
빌더(Builder) 패턴 예제 (0) | 2021.09.26 |
추상 팩토리(Abstract Factory)패턴 예제 (0) | 2021.09.26 |