Skip to content

Commit e3b1540

Browse files
committed
Implements Chain of Responsibility
1 parent c9a014d commit e3b1540

File tree

7 files changed

+131
-86
lines changed

7 files changed

+131
-86
lines changed

README.md

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

3-
## About
3+
## Chain of Responsibility
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+
Chain of Responsibility method gives to one object the responsibility of dealing with a request. If this object is not responsible for that request, it will pass it foward until the end of the chain.
76

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-
- [ ] Chain of responsability
17-
- [ ] Command
18-
- [ ] Iterator
19-
- [ ] Memento
20-
- [ ] Observer
21-
- [x] [State](https://github.com/LBeghini/Java-Design-Patterns/tree/3-state)
22-
- [ ] Strategy
23-
- [ ] 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-
- [ ] Adapter
36-
- [ ] 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)
7+
For example, when we go to a restaurant, we don't personally ask for the meal to the chef. We will ask the waiter, that will give the chef our request.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.language.programming.model;
2+
3+
import com.language.programming.model.enumerations.OrderType;
4+
5+
public class Chef extends Employee {
6+
7+
public Chef(Employee superior) {
8+
orderType = OrderType.MEAL;
9+
setSuperior(superior);
10+
}
11+
12+
@Override
13+
public String getOrder() {
14+
return "🍲";
15+
}
16+
17+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.language.programming.model;
2+
3+
import com.language.programming.model.enumerations.OrderType;
4+
5+
public abstract class Employee {
6+
7+
protected OrderType orderType;
8+
private Employee superior;
9+
10+
public Employee getSuperior() {
11+
return superior;
12+
}
13+
14+
public void setSuperior(Employee superior) {
15+
this.superior = superior;
16+
}
17+
18+
public abstract String getOrder();
19+
20+
public String order(OrderType order) {
21+
if (orderType == order) {
22+
return getOrder();
23+
} else {
24+
if (superior != null) {
25+
return superior.order(order);
26+
} else {
27+
return "";
28+
}
29+
}
30+
}
31+
32+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.language.programming.model;
2+
3+
import com.language.programming.model.enumerations.OrderType;
4+
5+
public class KitchenAssistant extends Employee {
6+
7+
public KitchenAssistant(Employee superior) {
8+
orderType = OrderType.DISH;
9+
setSuperior(superior);
10+
}
11+
12+
@Override
13+
public String getOrder() {
14+
return "🍽️";
15+
}
16+
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.language.programming.model;
2+
3+
import com.language.programming.model.enumerations.OrderType;
4+
5+
public class Waiter extends Employee {
6+
7+
public Waiter(Employee superior) {
8+
orderType = OrderType.MENU;
9+
setSuperior(superior);
10+
}
11+
12+
@Override
13+
public String getOrder() {
14+
return "📖";
15+
}
16+
17+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.language.programming.model.enumerations;
2+
3+
public enum OrderType {
4+
MENU, DISH, MEAL;
5+
6+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.enumerations.OrderType;
8+
9+
import org.junit.jupiter.api.BeforeEach;
10+
11+
public class OrderTest {
12+
13+
Waiter waiter;
14+
KitchenAssistant kitchenAssistant;
15+
Chef chef;
16+
17+
@BeforeEach
18+
void setUp() {
19+
chef = new Chef(null);
20+
kitchenAssistant = new KitchenAssistant(chef);
21+
waiter = new Waiter(kitchenAssistant);
22+
}
23+
24+
@Test
25+
public void shouldReturnMenu() {
26+
assertEquals("📖", waiter.order(OrderType.MENU));
27+
}
28+
29+
@Test
30+
public void shouldReturnDish() {
31+
assertEquals("🍽️", waiter.order(OrderType.DISH));
32+
}
33+
34+
@Test
35+
public void shouldReturnMeal() {
36+
assertEquals("🍲", waiter.order(OrderType.MEAL));
37+
}
38+
39+
}

0 commit comments

Comments
 (0)