-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDecoratorPattern.java
77 lines (61 loc) · 1.44 KB
/
DecoratorPattern.java
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
interface Pizza {
public String getDescription();
public double getCost();
}
abstract class PizzaDecorator implements Pizza {
Pizza pizza;
PizzaDecorator(Pizza simplePizza) {
this.pizza = simplePizza;
}
@Override
public String getDescription() {
return pizza.getDescription();
}
@Override
public double getCost() {
return pizza.getCost();
}
}
class SimplePizza implements Pizza {
@Override
public String getDescription() {
return "Dough";
}
@Override
public double getCost() {
return 100.0;
}
}
class MozzPizza extends PizzaDecorator {
MozzPizza(Pizza pizza) {
super(pizza);
}
@Override
public String getDescription() {
return super.getDescription() + ", Mozz";
}
@Override
public double getCost() {
return super.getCost() + 50;
}
}
class TomatoSaucePizza extends PizzaDecorator {
TomatoSaucePizza(Pizza pizza) {
super(pizza);
}
@Override
public String getDescription() {
return super.getDescription() + ", Tomato Sauce";
}
@Override
public double getCost() {
return super.getCost() + 10;
}
}
public class Suraj {
public static void main(String[] args) {
TomatoSaucePizza p = new TomatoSaucePizza(new MozzPizza(new SimplePizza()));
System.out.println(p.getDescription());
System.out.println(p.getCost());
}
}