Skip to content

Commit 193a996

Browse files
authored
Add classical decorator implementation (#43)
1 parent 48c9fb7 commit 193a996

File tree

7 files changed

+93
-0
lines changed

7 files changed

+93
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package pl.mperor.lab.java.design.pattern.structural.decorator.classic;
2+
3+
public interface Coffee {
4+
5+
int getCost();
6+
7+
default String getDescription() {
8+
return this.getClass().getSimpleName();
9+
}
10+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package pl.mperor.lab.java.design.pattern.structural.decorator.classic;
2+
3+
public abstract class CoffeeDecorator implements Coffee {
4+
5+
protected final Coffee coffee;
6+
protected final int cost;
7+
8+
protected CoffeeDecorator(Coffee coffee, int cost) {
9+
this.coffee = coffee;
10+
this.cost = cost;
11+
}
12+
13+
@Override
14+
public int getCost() {
15+
return coffee.getCost() + cost;
16+
}
17+
18+
@Override
19+
public String getDescription() {
20+
return "%s + %s".formatted(coffee.getDescription(), Coffee.super.getDescription());
21+
}
22+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package pl.mperor.lab.java.design.pattern.structural.decorator.classic;
2+
3+
public class NormalCoffee implements Coffee {
4+
5+
@Override
6+
public int getCost() {
7+
return 100;
8+
}
9+
10+
@Override
11+
public String getDescription() {
12+
return "Coffee";
13+
}
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package pl.mperor.lab.java.design.pattern.structural.decorator.classic;
2+
3+
public class WithChocolate extends CoffeeDecorator {
4+
5+
public WithChocolate(Coffee coffee) {
6+
super(coffee, 150);
7+
}
8+
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package pl.mperor.lab.java.design.pattern.structural.decorator.classic;
2+
3+
public class WithMilk extends CoffeeDecorator {
4+
5+
public WithMilk(Coffee coffee) {
6+
super(coffee, 50);
7+
}
8+
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package pl.mperor.lab.java.design.pattern.structural.decorator.classic;
2+
3+
public class WithSugar extends CoffeeDecorator {
4+
5+
public WithSugar(Coffee coffee) {
6+
super(coffee, 30);
7+
}
8+
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package pl.mperor.lab.java.design.pattern.structural.decorator.classic;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.stream.Stream;
7+
8+
public class CoffeeDecoratorTest {
9+
10+
@Test
11+
public void shouldAllowToDynamicallyExtendCoffeeWithMilkChocolateOrSugar() {
12+
NormalCoffee normal = new NormalCoffee();
13+
Coffee robustCoffee = new WithChocolate(new WithMilk(new WithSugar(normal)));
14+
Assertions.assertTrue(normal.getCost() < robustCoffee.getCost());
15+
Assertions.assertTrue(Stream.of("WithChocolate", "WithSugar", "WithMilk")
16+
.allMatch(robustCoffee.getDescription()::contains)
17+
);
18+
}
19+
20+
}

0 commit comments

Comments
 (0)