카테고리 없음

디자인패턴_데코레이터

Justin Mendes 2021. 7. 15. 11:24

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package org.example;
 
//음료의 추상 클래스
abstract class Beverage{
    String description = "알 수 없는 음료";
 
    public String getDescription(){
        return description;
    }
    public abstract double cost();
}
//음료-에스프레소
class Espresso extends Beverage {
    public Espresso() {
        description = "Espresso";
    }
    public double cost() {
        return 1.99;
    }
}
//음료-Decaf
class Decaf extends Beverage {
    public Decaf() {
        description="Decaf Coffee";
    }
    public  double cost(){
        return 1.05;
    }
}
//음료-하우스블랜드
class  HouseBlend extends Beverage {
    public HouseBlend() {
        description = "House Blend Coffee";
    }
    public double cost(){
        return .89;
    }
}
//음료-다크로스트
class DarkRoast extends Beverage {
    public DarkRoast() {
        description = "Dark Roast Coffee";
    }
    public double cost() {
        return .99;
    }
}
 
//추상클래스 - 추가 데코레이션
abstract class CondimentDecorator extends Beverage {
    Beverage beverage;
    public abstract String getDescription();
}
//데코레이션 - 우유
class Milk extends CondimentDecorator {
    public Milk(Beverage beverage) {
        this.beverage = beverage;
    }
    public String getDescription(){
        return beverage.getDescription() + ", Milk";
    }
    public double cost() {
        return .10 + beverage.cost();
    }
}
//데코레이션 - 모카
class Mocha extends CondimentDecorator {
    public Mocha(Beverage beverage) {
        this.beverage = beverage;
    }
    public String getDescription(){
        return beverage.getDescription() + ", Mocha";
    }
    public double cost() {
        return .20 + beverage.cost();
    }
}
//데코레이션 - 두유
class Soy extends CondimentDecorator {
    public Soy(Beverage beverage) {
        this.beverage = beverage;
    }
    public String getDescription(){
        return beverage.getDescription() + ", Soy";
    }
    public double cost() {
        return .15 + beverage.cost();
    }
}
//데코레이션 - 휘핑
class Whip extends CondimentDecorator {
    public Whip(Beverage beverage) {
        this.beverage = beverage;
    }
    public String getDescription(){
        return beverage.getDescription() + ", Whip";
    }
    public double cost() {
        return .10 + beverage.cost();
    }
}
 
 
 
public class Main {
    public static void main(String[] args) {
        Beverage beverage = new Espresso();
        System.out.println(beverage.getDescription()+" $ "
        +beverage.cost());
 
        Beverage beverage2 = new DarkRoast();
        beverage2 = new Mocha(beverage2);
        beverage2 = new Mocha(beverage2);
        beverage2 = new Whip(beverage2);
        System.out.println(beverage2.getDescription()+" $ "
        +beverage2.cost());
 
        Beverage beverage3 = new HouseBlend();
        beverage3 = new Soy(beverage3);
        beverage3 = new Mocha(beverage3);
        beverage3 = new Whip(beverage3);
        System.out.println(beverage3.getDescription()+" $ "
        + beverage3.cost());
 
    }
}
 
cs

 

 

커피 음료 가계로 데코레이터 패턴을 공부한 여러분에게 피자 가계를 만들어 달라는 의뢰가 왔습니다.

 

작은 피자 가계인 만큼 데코레이터 패턴의 핵심만 이용해도 구현될 만큼  

 

한번 활용하기로 마음을 먹고 직접 구현해 보기로 하였습니다.

 

피자(Pizza) --> 얇은 크러스트 피자(ThincrustPizza)     가격: $7.99

                  두꺼운 크러스트 피자(ThickcrustPizza)  가격: $7.99

 

토핑(ToppingDecorator) --> 치즈(Cheese) 가격: free $0

                                올리브(Olives) 가격: $0.3

 

 

치즈피자에 올리브를 올린 피자를 그리스피자(greekPizza)라고 합니다 

그리스 피자를 메인에서 만들어 가격을 출력해 주세요.

---------------------------------------------------

Thin crust pizza, with tomato sauce, Cheese, Olives $8.29 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package org.example;
 
import java.text.DecimalFormat;
 
abstract class Pizza {
    String description = "초기 피자";
 
    public String getDescription() {
        return description;
    }
    public  abstract double cost();
}
 
class ThincrustPizza extends Pizza {
    public ThincrustPizza() {
        description = "ThincrustPizza, with tomato sauce";
    }
    public  double cost() {
        return  7.99;
    }
}
 
class ThickcrustPizza extends Pizza {
    public ThickcrustPizza() {
        description = "ThickcrustPizza, with tomato sauce";
    }
    public  double cost() {
        return 7.99;
    }
}
 
abstract class Topping extends Pizza {
    Pizza pizza;
    public abstract String getDescription();
}
 
 
class Cheese extends Topping {
    public Cheese(Pizza pizza) {
        this.pizza = pizza;
    }
    public  String getDescription() {
        return pizza.getDescription() + ", Cheese";
    }
    public double cost() {
        return .0 + pizza.cost();
    }
}
 
class Olives extends Topping {
    public Olives(Pizza pizza) {
        this.pizza = pizza;
    }
    public  String getDescription() {
        return pizza.getDescription() + ", Olives";
    }
    public double cost() {
        return .3 + pizza.cost();
    }
}
 
public class Main {
    public static void main(String[] args) {
        Pizza pizza = new ThincrustPizza();
        Pizza cheesePizza = new Cheese(pizza);
        Pizza greekPizza = new Olives(cheesePizza);
 
        DecimalFormat decimal = new DecimalFormat("#.###");
        System.out.println(greekPizza.getDescription()
                + " $" + decimal.format(greekPizza.cost()));
 
    }
}
 
cs