Skip to content

Commit a8efa0b

Browse files
committed
Implements strategy
1 parent d7ad582 commit a8efa0b

8 files changed

Lines changed: 128 additions & 83 deletions

File tree

README.md

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

3-
## About
3+
## Strategy
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+
Strategy pattern lets the implementation of the method vary independently for whoever uses it.
76

8-
The main branch is just a template for every other branch.
7+
For example, let's say we have an app that calculates the shortest path depending of the means of transportation.
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+
- If you are going on foot, you want the shortest walking path.
10+
- If you are going by car, you want the shortest driving path.
11+
- If you are going by bicycle, you want the shortest cycling path.
1112

12-
## Implemented design patterns
13+
The calculation algorithms of each method may vary. On foot, it might be benefitial to search for paths that doesn't have many climbs. By car, traffic must be taken under consideration. By bicycle, the path must be calculated throught streets that have a bike lane.
1314

14-
### Behavioural patterns
15-
16-
- [x] [Chain of responsibility](https://github.com/LBeghini/Java-Design-Patterns/tree/4-chain-of-responsibility)
17-
- [ ] 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-
- [ ] 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-
- [ ] 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)
15+
So each one of these calculations would be a different strategy.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.language.programming.model;
2+
3+
public class App {
4+
5+
public String calculate(Strategy strategy) {
6+
return strategy.calculate();
7+
}
8+
9+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.language.programming.model;
2+
3+
import com.language.programming.model.strategies.Bicycle;
4+
import com.language.programming.model.strategies.Car;
5+
import com.language.programming.model.strategies.Pedestrian;
6+
7+
public class Person {
8+
9+
private String path;
10+
11+
public String getPath() {
12+
return path;
13+
}
14+
15+
public void walk() {
16+
App app = new App();
17+
this.path = app.calculate(new Pedestrian());
18+
19+
}
20+
21+
public void drive() {
22+
App app = new App();
23+
this.path = app.calculate(new Car());
24+
25+
}
26+
27+
public void rideBike() {
28+
App app = new App();
29+
this.path = app.calculate(new Bicycle());
30+
31+
}
32+
33+
}
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 Strategy {
4+
5+
String calculate();
6+
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.language.programming.model.strategies;
2+
3+
import com.language.programming.model.Strategy;
4+
5+
public class Bicycle implements Strategy {
6+
@Override
7+
public String calculate() {
8+
return "Shortest cycling path";
9+
}
10+
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.language.programming.model.strategies;
2+
3+
import com.language.programming.model.Strategy;
4+
5+
public class Car implements Strategy {
6+
7+
@Override
8+
public String calculate() {
9+
return "Shortest driving path";
10+
}
11+
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.language.programming.model.strategies;
2+
3+
import com.language.programming.model.Strategy;
4+
5+
public class Pedestrian implements Strategy {
6+
7+
@Override
8+
public String calculate() {
9+
return "Shortest walking path";
10+
}
11+
12+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 org.junit.jupiter.api.BeforeEach;
8+
9+
public class PersonTest {
10+
11+
Person person;
12+
13+
@BeforeEach
14+
private void setUp() {
15+
person = new Person();
16+
}
17+
18+
@Test
19+
public void shouldReturnPedestrianPath() {
20+
person.walk();
21+
assertEquals(person.getPath(), "Shortest walking path");
22+
}
23+
24+
@Test
25+
public void shouldReturnCarPath() {
26+
person.drive();
27+
assertEquals(person.getPath(), "Shortest driving path");
28+
}
29+
30+
@Test
31+
public void shouldReturnBicyclePath() {
32+
person.rideBike();
33+
assertEquals(person.getPath(), "Shortest cycling path");
34+
}
35+
36+
}

0 commit comments

Comments
 (0)