Skip to content

Commit bdfc9b5

Browse files
committed
Implements decorator
1 parent dbc21a1 commit bdfc9b5

File tree

9 files changed

+184
-86
lines changed

9 files changed

+184
-86
lines changed

README.md

Lines changed: 8 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,14 @@
11
# ☕ Java Design Patterns
22

3-
## About
3+
## Decorator
44

5-
This application is a simple Java Console Application that aims to implement design pattern examples to a Programmin Language Class. A set of design patterns will be developed
6-
each week, and they are divided in different [branches](https://github.com/LBeghini/Java-Design-Patterns/branches).
5+
Decorator pattern attach additional responsibilities to an object dynamically.
76

8-
The main branch is just a template for every other branch.
7+
For example, we are a baker we have some differente cake decorations:
98

10-
Also, to make it easier to download the source code, [releases](https://github.com/LBeghini/Java-Design-Patterns/releases) are created related to the task of the week, giving a snapshot of the code for that specific implementation.
9+
- Buttercream
10+
- Ganache
11+
- Meringue
12+
- Mousse
1113

12-
## Implemented design patterns
13-
14-
### Behavioural patterns
15-
16-
- [x] [Chain of responsibility](https://github.com/LBeghini/Java-Design-Patterns/tree/4-chain-of-responsibility)
17-
- [x] [Command](https://github.com/LBeghini/Java-Design-Patterns/tree/6-command)
18-
- [x] [Interpreter](https://github.com/LBeghini/Java-Design-Patterns/tree/9-interpreter)
19-
- [x] [Iterator](https://github.com/LBeghini/Java-Design-Patterns/tree/4-iterator)
20-
- [x] [Mediator](https://github.com/LBeghini/Java-Design-Patterns/tree/9-mediator)
21-
- [x] [Memento](https://github.com/LBeghini/Java-Design-Patterns/tree/5-memento)
22-
- [x] [Observer](https://github.com/LBeghini/Java-Design-Patterns/tree/5-observer)
23-
- [x] [State](https://github.com/LBeghini/Java-Design-Patterns/tree/3-state)
24-
- [x] [Strategy](https://github.com/LBeghini/Java-Design-Patterns/tree/6-strategy)
25-
- [x] [Template method](https://github.com/LBeghini/Java-Design-Patterns/tree/4-template-method)
26-
27-
### Creational patterns
28-
29-
- [ ] Abstract factory
30-
- [x] [Builder](https://github.com/LBeghini/Java-Design-Patterns/tree/1-builder)
31-
- [x] [Factory method](https://github.com/LBeghini/Java-Design-Patterns/tree/2-factory-method)
32-
- [x] [Prototype](https://github.com/LBeghini/Java-Design-Patterns/tree/2-prototype)
33-
- [x] [Singleton](https://github.com/LBeghini/Java-Design-Patterns/tree/1-singleton)
34-
35-
### Structural patterns
36-
37-
- [x] [Adapter](https://github.com/LBeghini/Java-Design-Patterns/tree/7-adapter)
38-
- [x] [Bridge](https://github.com/LBeghini/Java-Design-Patterns/tree/7-bridge)
39-
- [x] [Composite](https://github.com/LBeghini/Java-Design-Patterns/tree/8-composite)
40-
- [ ] Decorator
41-
- [x] [Facade](https://github.com/LBeghini/Java-Design-Patterns/tree/8-facade)
42-
- [x] [Flyweight](https://github.com/LBeghini/Java-Design-Patterns/tree/10-flyweight)
43-
- [x] [Proxy](https://github.com/LBeghini/Java-Design-Patterns/tree/10-proxy)
44-
45-
46-
## Technologies
47-
48-
- Java
49-
- JUnit
50-
- Maven
51-
52-
## Requirements
53-
54-
To run and edit the project, be sure to have installed in your computer the following softwares:
55-
- A code editor
56-
57-
After that, you'll need to clone this repo:
58-
59-
```bash
60-
git clone https://github.com/LBeghini/Java-Design-Patterns.git
61-
```
62-
63-
## Change branch
64-
65-
To change to a different branch, run the command:
66-
67-
```bash
68-
git checkout name-of-the-branch
69-
```
70-
71-
The branch names have the pattern:
72-
73-
```bash
74-
{number-of-the-week}-{pattern-name}
75-
```
76-
77-
> `number-of-the-week` corresponds to the week asked to be implemented certain pattern
78-
79-
## Testing
80-
81-
This project has no aim to run any of the implemented classes, as the goal is the code itself. However, the classes will be tested to visualize the behaviour and implementation
82-
of the patterns.
83-
84-
You can run the tests using the maven wrapper:
85-
86-
```bash
87-
./mvnw test
88-
```
89-
90-
## :balance_scale: License
91-
92-
[MIT License](https://github.com/LBeghini/Java-Design-Patterns/blob/main/LICENSE)
14+
The design pattern allow us to mix any of these decorations upon a cake.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.language.programming.model;
2+
3+
public class Cake implements CakePan {
4+
5+
public Cake() {
6+
}
7+
8+
@Override
9+
public String getAdornmentStructure() {
10+
return "Cake";
11+
}
12+
13+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.language.programming.model;
2+
3+
public abstract class CakeDecorator implements CakePan {
4+
5+
private CakePan cake;
6+
public String adornment;
7+
8+
public CakeDecorator(CakePan cake) {
9+
this.cake = cake;
10+
}
11+
12+
public CakePan getCake() {
13+
return cake;
14+
}
15+
16+
public void setCake(CakePan cake) {
17+
this.cake = cake;
18+
}
19+
20+
public abstract String getAdornment();
21+
22+
public void setAdornment(String adornment) {
23+
this.adornment = adornment;
24+
}
25+
26+
public String getAdornmentStructure() {
27+
return this.cake.getAdornmentStructure() + "/" + this.getAdornment();
28+
}
29+
30+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.language.programming.model;
2+
3+
public interface CakePan {
4+
5+
String getAdornmentStructure();
6+
7+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.language.programming.model.decorations;
2+
3+
import com.language.programming.model.CakePan;
4+
import com.language.programming.model.CakeDecorator;
5+
6+
public class Buttercream extends CakeDecorator {
7+
8+
public Buttercream(CakePan cake) {
9+
super(cake);
10+
}
11+
12+
@Override
13+
public String getAdornment() {
14+
return "Buttercream";
15+
}
16+
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.language.programming.model.decorations;
2+
3+
import com.language.programming.model.CakePan;
4+
import com.language.programming.model.CakeDecorator;
5+
6+
public class Ganache extends CakeDecorator {
7+
8+
public Ganache(CakePan cake) {
9+
super(cake);
10+
}
11+
12+
@Override
13+
public String getAdornment() {
14+
return "Ganache";
15+
}
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.language.programming.model.decorations;
2+
3+
import com.language.programming.model.CakePan;
4+
import com.language.programming.model.CakeDecorator;
5+
6+
public class Meringue extends CakeDecorator {
7+
8+
public Meringue(CakePan cake) {
9+
super(cake);
10+
}
11+
12+
@Override
13+
public String getAdornment() {
14+
return "Meringue";
15+
}
16+
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.language.programming.model.decorations;
2+
3+
import com.language.programming.model.CakePan;
4+
import com.language.programming.model.CakeDecorator;
5+
6+
public class Mousse extends CakeDecorator {
7+
8+
public Mousse(CakePan cake) {
9+
super(cake);
10+
}
11+
12+
@Override
13+
public String getAdornment() {
14+
return "Mousse";
15+
}
16+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.language.programming.model;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
import com.language.programming.model.decorations.*;
8+
9+
public class CakeTest {
10+
11+
@Test
12+
public void shouldReturnCake() {
13+
CakePan cake = new Cake();
14+
15+
assertEquals("Cake", cake.getAdornmentStructure());
16+
17+
}
18+
19+
@Test
20+
public void shouldReturnButtercreamCake() {
21+
CakePan cake = new Buttercream(new Cake());
22+
23+
assertEquals("Cake/Buttercream", cake.getAdornmentStructure());
24+
25+
}
26+
27+
@Test
28+
public void shouldReturnGanacheCake() {
29+
CakePan cake = new Ganache(new Cake());
30+
31+
assertEquals("Cake/Ganache", cake.getAdornmentStructure());
32+
33+
}
34+
35+
@Test
36+
public void shouldReturnMeringueCake() {
37+
CakePan cake = new Meringue(new Cake());
38+
39+
assertEquals("Cake/Meringue", cake.getAdornmentStructure());
40+
41+
}
42+
43+
@Test
44+
public void shouldReturnMousseCake() {
45+
CakePan cake = new Mousse(new Cake());
46+
47+
assertEquals("Cake/Mousse", cake.getAdornmentStructure());
48+
49+
}
50+
51+
@Test
52+
public void shouldReturnGanacheMousseCake() {
53+
CakePan cake = new Mousse(new Cake());
54+
55+
assertEquals("Cake/Mousse", cake.getAdornmentStructure());
56+
57+
}
58+
59+
60+
}

0 commit comments

Comments
 (0)