Skip to content

Commit 2625ee0

Browse files
committed
Implements mediator
1 parent 4d5a1ae commit 2625ee0

6 files changed

Lines changed: 109 additions & 86 deletions

File tree

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+
## Mediator
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+
Mediator pattern defines an interface to communicate with other objects.
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-
- [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-
- [x] [Composite](https://github.com/LBeghini/Java-Design-Patterns/tree/8-composite)
38-
- [ ] Decorator
39-
- [x] [Facade](https://github.com/LBeghini/Java-Design-Patterns/tree/8-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, let's say that we have to get a Defaut Gateway address from our router in order to communicate in the internet. A host computer does not directly communicates with the router. To do so, it needs to send requests throug protocols that will provide the proper answer. The request and protocol is our mediator to the router.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.language.model;
2+
3+
public class Host {
4+
5+
public Host() {
6+
7+
}
8+
9+
public String requestDeviceDNSAddress() {
10+
return Request.getInstance().requestDeviceMacAddress();
11+
}
12+
13+
public String requestDefaultGateway(String network) {
14+
return Request.getInstance().requestDefaultGateway(network);
15+
}
16+
17+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.language.model;
2+
3+
public interface Protocol {
4+
String requestDNS();
5+
6+
String requestDefaultGateway(String network);
7+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.language.model;
2+
3+
public class Request {
4+
5+
private static Request instance = new Request();
6+
7+
private Request() {
8+
}
9+
10+
public static Request getInstance() {
11+
return instance;
12+
}
13+
14+
public String requestDeviceMacAddress() {
15+
return "Sending request... \n Response: " + Router.getInstance().requestDNS();
16+
}
17+
18+
public String requestDefaultGateway(String network) {
19+
return "Sending request... \n Response: " + Router.getInstance().requestDefaultGateway(network);
20+
}
21+
22+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.language.model;
2+
3+
public class Router implements Protocol {
4+
5+
private static Router instance = new Router();
6+
7+
private Router() {
8+
};
9+
10+
public static Router getInstance() {
11+
return instance;
12+
}
13+
14+
@Override
15+
public String requestDNS() {
16+
return "DNS ADDRESS: 201.291.20.3";
17+
}
18+
19+
@Override
20+
public String requestDefaultGateway(String network) {
21+
if (network.contains("192.168")) {
22+
return "IP: 192.168.0.1";
23+
}
24+
25+
if (network.contains("172.16")) {
26+
return "IP: 192.168.0.1";
27+
}
28+
29+
if (network.contains("10")) {
30+
return "IP: 192.168.0.1";
31+
}
32+
33+
return "APIPA: 169.254.0.1";
34+
}
35+
36+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.language.programming.model;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
import com.language.model.Host;
7+
8+
public class HostTest {
9+
10+
@Test
11+
public void shouldRequestDNS() {
12+
Host host = new Host();
13+
assertEquals("Sending request... \n Response: DNS ADDRESS: 201.291.20.3", host.requestDeviceDNSAddress());
14+
15+
}
16+
17+
@Test
18+
public void shouldRequestDefaultGateway() {
19+
Host host = new Host();
20+
assertEquals("Sending request... \n Response: IP: 192.168.0.1", host.requestDefaultGateway("192.168.0.5"));
21+
22+
}
23+
24+
}

0 commit comments

Comments
 (0)