Skip to content

Commit 70f70e0

Browse files
committed
Implements bridge
1 parent 730d0ce commit 70f70e0

File tree

10 files changed

+161
-82
lines changed

10 files changed

+161
-82
lines changed

README.md

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

3-
## About
3+
## Bridge
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+
Bridge pattern separate the abstraction from the implementation, allowing them to be developed idependently and let the access to the abstraction part dissociate from the implementation part.
76

8-
The main branch is just a template for every other branch.
7+
For example, lets say that we have a factory that makes two types of candy: cakes and ice cream. That's our abstraction.
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+
For each candy, there's two flavours: chocolate and strawberry. That's our interface.
1110

12-
## Implemented design patterns
11+
The bridge is between each candy with the flavours.
1312

14-
### Behavioural patterns
13+
<img src="./resources/BridgeDiagram.png" width="800px" />
1514

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-
- [ ] 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)

resources/BridgeDiagram.png

18.2 KB
Loading
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.language.programming.model;
2+
3+
public abstract class Candy {
4+
5+
protected Flavor flavor;
6+
protected float sugar;
7+
8+
public Candy() {
9+
this.sugar = 50f;
10+
}
11+
12+
public void setFlavor(Flavor flavor) {
13+
this.flavor = flavor;
14+
}
15+
16+
public abstract float getSugar();
17+
18+
}
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 Flavor {
4+
5+
float sugar();
6+
7+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.language.programming.model.candies;
2+
3+
import com.language.programming.model.Candy;
4+
5+
public class Cake extends Candy {
6+
7+
public Cake() {
8+
super();
9+
}
10+
11+
@Override
12+
public float getSugar() {
13+
return this.sugar - this.flavor.sugar();
14+
}
15+
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.language.programming.model.candies;
2+
3+
import com.language.programming.model.Candy;
4+
5+
public class IceCream extends Candy {
6+
7+
public IceCream() {
8+
super();
9+
}
10+
11+
@Override
12+
public float getSugar() {
13+
return this.sugar;
14+
}
15+
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.language.programming.model.flavors;
2+
3+
import com.language.programming.model.Flavor;
4+
5+
public class Chocolate implements Flavor {
6+
7+
@Override
8+
public float sugar() {
9+
return 30f;
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.flavors;
2+
3+
import com.language.programming.model.Flavor;
4+
5+
public class Strawberry implements Flavor {
6+
7+
@Override
8+
public float sugar() {
9+
return 20f;
10+
}
11+
12+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.language.programming.model.candies;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
import com.language.programming.model.Candy;
8+
import com.language.programming.model.Flavor;
9+
import com.language.programming.model.flavors.Chocolate;
10+
import com.language.programming.model.flavors.Strawberry;
11+
12+
import org.junit.jupiter.api.BeforeEach;
13+
14+
public class CakeTest {
15+
16+
Candy cake;
17+
18+
@BeforeEach
19+
public void setup() {
20+
cake = new Cake();
21+
}
22+
23+
@Test
24+
public void shouldReturnChocolateCake() {
25+
Flavor flavor = new Chocolate();
26+
cake.setFlavor(flavor);
27+
assertEquals(20f, cake.getSugar());
28+
}
29+
30+
@Test
31+
public void shouldReturnStrawberryCake() {
32+
Flavor flavor = new Strawberry();
33+
cake.setFlavor(flavor);
34+
assertEquals(30f, cake.getSugar());
35+
}
36+
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.language.programming.model.candies;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
import com.language.programming.model.Candy;
8+
import com.language.programming.model.Flavor;
9+
import com.language.programming.model.flavors.Chocolate;
10+
import com.language.programming.model.flavors.Strawberry;
11+
12+
import org.junit.jupiter.api.BeforeEach;
13+
14+
public class IceCreamTest {
15+
16+
Candy iceCream;
17+
18+
@BeforeEach
19+
public void setup() {
20+
iceCream = new IceCream();
21+
}
22+
23+
@Test
24+
public void shouldReturnChocolateIceCream() {
25+
Flavor flavor = new Chocolate();
26+
iceCream.setFlavor(flavor);
27+
assertEquals(50f, iceCream.getSugar());
28+
}
29+
30+
@Test
31+
public void shouldReturnStrawberryIceCream() {
32+
Flavor flavor = new Strawberry();
33+
iceCream.setFlavor(flavor);
34+
assertEquals(50f, iceCream.getSugar());
35+
}
36+
37+
}

0 commit comments

Comments
 (0)