-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDecorator.java
56 lines (44 loc) · 1.17 KB
/
Decorator.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
package code;
public class Decorator {
public static void main(String[] args) {
Coffee coffee = new Coffee();
System.out.println(coffee.info());
SugaryDrink sugary_coffee = new SugaryDrink(coffee, 10);
System.out.println(sugary_coffee.info());
ColoredDrink colored_sugary_coffee = new ColoredDrink(sugary_coffee, "red");
System.out.println(colored_sugary_coffee.info());
}
}
interface Drink {
String info();
}
class Coffee implements Drink {
@Override
public String info() {
return "Coffee";
}
}
class SugaryDrink implements Drink {
private Drink drink;
private int sugar;
public SugaryDrink(Drink drink, int sugar) {
this.drink = drink;
this.sugar = sugar;
}
@Override
public String info() {
return drink.info() + " with " + sugar + "g of sugar";
}
}
class ColoredDrink implements Drink {
private Drink drink;
private String color;
public ColoredDrink(Drink drink, String color) {
this.drink = drink;
this.color = color;
}
@Override
public String info() {
return drink.info() + " with color " + color;
}
}