Skip to content

Commit 0794f54

Browse files
committed
Instantiate Composite
1 parent e3cf248 commit 0794f54

File tree

6 files changed

+116
-88
lines changed

6 files changed

+116
-88
lines changed

README.md

Lines changed: 1 addition & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,3 @@
11
# ☕ Java Design Patterns
22

3-
## About
4-
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).
7-
8-
The main branch is just a template for every other branch.
9-
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.
11-
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] [Iterator](https://github.com/LBeghini/Java-Design-Patterns/tree/4-iterator)
19-
- [x] [Memento](https://github.com/LBeghini/Java-Design-Patterns/tree/5-memento)
20-
- [x] [Observer](https://github.com/LBeghini/Java-Design-Patterns/tree/5-observer)
21-
- [x] [State](https://github.com/LBeghini/Java-Design-Patterns/tree/3-state)
22-
- [x] [Strategy](https://github.com/LBeghini/Java-Design-Patterns/tree/6-strategy)
23-
- [x] [Template method](https://github.com/LBeghini/Java-Design-Patterns/tree/4-template-method)
24-
25-
### Creational patterns
26-
27-
- [ ] Abstract factory
28-
- [x] [Builder](https://github.com/LBeghini/Java-Design-Patterns/tree/1-builder)
29-
- [x] [Factory method](https://github.com/LBeghini/Java-Design-Patterns/tree/2-factory-method)
30-
- [x] [Prototype](https://github.com/LBeghini/Java-Design-Patterns/tree/2-prototype)
31-
- [x] [Singleton](https://github.com/LBeghini/Java-Design-Patterns/tree/1-singleton)
32-
33-
### Structural patterns
34-
35-
- [x] [Adapter](https://github.com/LBeghini/Java-Design-Patterns/tree/7-adapter)
36-
- [x] [Bridge](https://github.com/LBeghini/Java-Design-Patterns/tree/7-bridge)
37-
- [ ] Composite
38-
- [ ] Decorator
39-
- [ ] Facade
40-
- [ ] Flyweight
41-
- [ ] Mediator
42-
- [ ] Proxy
43-
44-
## Technologies
45-
46-
- Java
47-
- JUnit
48-
- Maven
49-
50-
## Requirements
51-
52-
To run and edit the project, be sure to have installed in your computer the following softwares:
53-
- A code editor
54-
55-
After that, you'll need to clone this repo:
56-
57-
```bash
58-
git clone https://github.com/LBeghini/Java-Design-Patterns.git
59-
```
60-
61-
## Change branch
62-
63-
To change to a different branch, run the command:
64-
65-
```bash
66-
git checkout name-of-the-branch
67-
```
68-
69-
The branch names have the pattern:
70-
71-
```bash
72-
{number-of-the-week}-{pattern-name}
73-
```
74-
75-
> `number-of-the-week` corresponds to the week asked to be implemented certain pattern
76-
77-
## Testing
78-
79-
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
80-
of the patterns.
81-
82-
You can run the tests using the maven wrapper:
83-
84-
```bash
85-
./mvnw test
86-
```
87-
88-
## :balance_scale: License
89-
90-
[MIT License](https://github.com/LBeghini/Java-Design-Patterns/blob/main/LICENSE)
3+
## Composite
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.language.programming.src;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Box extends Package {
7+
8+
private List<Package> content;
9+
10+
public Box(String description) {
11+
super(description);
12+
this.content = new ArrayList<>();
13+
}
14+
15+
public void addContent(Package content) {
16+
this.content.add(content);
17+
}
18+
19+
@Override
20+
public String getContent() {
21+
StringBuilder output = new StringBuilder();
22+
output.append(this.getDescription() + "\n");
23+
for (Package content : content) {
24+
output.append(content.getContent() + "\n");
25+
}
26+
return output.toString();
27+
}
28+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.language.programming.src;
2+
3+
public class Item extends Package {
4+
5+
public Item(String description) {
6+
super(description);
7+
}
8+
9+
@Override
10+
public String getContent() {
11+
return this.getDescription();
12+
}
13+
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.language.programming.src;
2+
3+
public class Order {
4+
private Package pack;
5+
6+
public void setPackage(Package pack) {
7+
this.pack = pack;
8+
}
9+
10+
public String getPackage() {
11+
if (this.pack == null) {
12+
throw new NullPointerException();
13+
}
14+
return this.pack.getContent();
15+
}
16+
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.language.programming.src;
2+
3+
public abstract class Package {
4+
5+
private String description;
6+
7+
public Package(String description) {
8+
this.description = description;
9+
}
10+
11+
public String getDescription() {
12+
return description;
13+
}
14+
15+
public void setDescription(String description) {
16+
this.description = description;
17+
}
18+
19+
public abstract String getContent();
20+
21+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.language.programming.src;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
public class OrderTest {
8+
9+
@Test
10+
public void shouldReturnOrderPackage() {
11+
Box box1 = new Box("Bubble Wrap");
12+
13+
Box box2 = new Box("Sensitive content");
14+
Item item21 = new Item("Smartphone");
15+
box2.addContent(item21);
16+
17+
Box box3 = new Box("Random content");
18+
Item item31 = new Item("Book");
19+
Item item32 = new Item("Shovel");
20+
box3.addContent(item31);
21+
box3.addContent(item32);
22+
23+
Box pack = new Box("Package");
24+
pack.addContent(box1);
25+
pack.addContent(box2);
26+
pack.addContent(box3);
27+
28+
Order order = new Order();
29+
order.setPackage(pack);
30+
31+
assertEquals("Package\n" + "Bubble Wrap\n" + "\n" + "Sensitive content\n" + "Smartphone\n" + "\n"
32+
+ "Random content\n" + "Book\n" + "Shovel\n" + "\n", order.getPackage());
33+
}
34+
35+
}

0 commit comments

Comments
 (0)