Skip to content

Commit 78f107a

Browse files
committed
Implements proxy
1 parent 4504cfd commit 78f107a

File tree

9 files changed

+198
-88
lines changed

9 files changed

+198
-88
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,6 @@ buildNumber.properties
5454
.history
5555
.ionide
5656

57-
# End of https://www.toptal.com/developers/gitignore/api/java,maven,visualstudiocode
57+
# End of https://www.toptal.com/developers/gitignore/api/java,maven,visualstudiocode
58+
59+
.vscode

README.md

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

3-
## About
3+
## Proxy
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+
Proxy pattern provides a substitute for another object, so it can controls the access to the original project, allowing actions to be performed either before or after the request gets through the original object.
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-
- [x] [Mediator](https://github.com/LBeghini/Java-Design-Patterns/tree/9-mediator)
25-
- [x] [Interpreter](https://github.com/LBeghini/Java-Design-Patterns/tree/9-interpreter)
26-
27-
### Creational patterns
28-
29-
- [ ] Abstract factory
30-
- [x] [Builder](https://github.com/LBeghini/Java-Design-Patterns/tree/1-builder)
31-
- [x] [Factory method](https://github.com/LBeghini/Java-Design-Patterns/tree/2-factory-method)
32-
- [x] [Prototype](https://github.com/LBeghini/Java-Design-Patterns/tree/2-prototype)
33-
- [x] [Singleton](https://github.com/LBeghini/Java-Design-Patterns/tree/1-singleton)
34-
35-
### Structural patterns
36-
37-
- [x] [Adapter](https://github.com/LBeghini/Java-Design-Patterns/tree/7-adapter)
38-
- [x] [Bridge](https://github.com/LBeghini/Java-Design-Patterns/tree/7-bridge)
39-
- [x] [Composite](https://github.com/LBeghini/Java-Design-Patterns/tree/8-composite)
40-
- [ ] Decorator
41-
- [x] [Facade](https://github.com/LBeghini/Java-Design-Patterns/tree/8-facade)
42-
- [ ] Flyweight
43-
- [ ] Proxy
44-
45-
## Technologies
46-
47-
- Java
48-
- JUnit
49-
- Maven
50-
51-
## Requirements
52-
53-
To run and edit the project, be sure to have installed in your computer the following softwares:
54-
- A code editor
55-
56-
After that, you'll need to clone this repo:
57-
58-
```bash
59-
git clone https://github.com/LBeghini/Java-Design-Patterns.git
60-
```
61-
62-
## Change branch
63-
64-
To change to a different branch, run the command:
65-
66-
```bash
67-
git checkout name-of-the-branch
68-
```
69-
70-
The branch names have the pattern:
71-
72-
```bash
73-
{number-of-the-week}-{pattern-name}
74-
```
75-
76-
> `number-of-the-week` corresponds to the week asked to be implemented certain pattern
77-
78-
## Testing
79-
80-
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
81-
of the patterns.
82-
83-
You can run the tests using the maven wrapper:
84-
85-
```bash
86-
./mvnw test
87-
```
88-
89-
## :balance_scale: License
90-
91-
[MIT License](https://github.com/LBeghini/Java-Design-Patterns/blob/main/LICENSE)
7+
For example, let's say we have a client that wants to connect to a server. Not every client can perform every action on a server, being required administrator access. In order to control that, we build a proxy and let the client communicate only with the proxy, so the proxy can determine if the client can perform the action or not.

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
<version>5.7.1</version>
3232
<scope>test</scope>
3333
</dependency>
34+
<dependency>
35+
<groupId>org.hamcrest</groupId>
36+
<artifactId>java-hamcrest</artifactId>
37+
<version>2.0.0.0</version>
38+
<scope>test</scope>
39+
</dependency>
3440
</dependencies>
3541

3642
<build>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.language.programming.model;
2+
3+
public class Client {
4+
5+
private String username;
6+
private Role role;
7+
8+
public Client(String username, Role role) {
9+
this.username = username;
10+
this.role = role;
11+
}
12+
13+
public String getUsername() {
14+
return username;
15+
}
16+
17+
public void setUsername(String username) {
18+
this.username = username;
19+
}
20+
21+
public Role getRole() {
22+
return role;
23+
}
24+
25+
public void setRole(Role role) {
26+
this.role = role;
27+
}
28+
29+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.language.programming.model;
2+
3+
public class Proxy implements Service {
4+
5+
private Server server;
6+
private String ip;
7+
8+
public Proxy(String ip) {
9+
this.ip = ip;
10+
}
11+
12+
@Override
13+
public void shutdown(Client client) {
14+
15+
if (!checkAccess(client)) {
16+
throw new IllegalAccessError("Unauthorized.");
17+
}
18+
19+
if (this.server == null) {
20+
this.server = new Server(this.ip);
21+
}
22+
23+
this.server.shutdown(client);
24+
}
25+
26+
@Override
27+
public String getInfo(Client client) {
28+
29+
if (this.server == null) {
30+
this.server = new Server(this.ip);
31+
}
32+
return this.server.getInfo(client);
33+
}
34+
35+
public boolean checkAccess(Client client) {
36+
if (!client.getRole().equals(Role.ADMIN)) {
37+
return false;
38+
}
39+
40+
return true;
41+
42+
}
43+
44+
@Override
45+
public boolean isPowered() {
46+
47+
if (this.server == null) {
48+
this.server = new Server(this.ip);
49+
}
50+
51+
return this.server.isPowered();
52+
}
53+
54+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.language.programming.model;
2+
3+
public enum Role {
4+
ADMIN, USER;
5+
6+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.language.programming.model;
2+
3+
import java.util.Random;
4+
5+
public class Server implements Service {
6+
7+
private String ip;
8+
private Integer memory;
9+
private Integer CPU;
10+
private Integer disk;
11+
private boolean power;
12+
13+
public Server(String ip) {
14+
Random random = new Random();
15+
16+
this.ip = ip;
17+
18+
this.memory = random.nextInt(8000);
19+
this.CPU = random.nextInt(100);
20+
this.disk = random.nextInt(100);
21+
this.power = true;
22+
23+
}
24+
25+
@Override
26+
public void shutdown(Client client) {
27+
this.power = false;
28+
}
29+
30+
@Override
31+
public String getInfo(Client client) {
32+
return "ipAddress: " + this.ip + "\nMemTotal: " + 8000 + "kB"
33+
+ "\nMemAvailable: " + (8000 - this.memory) + "kB" + "\ncpuUsage: "
34+
+ this.CPU + "%" + "\nDiskUsage: " + this.disk + "%";
35+
}
36+
37+
public boolean isPowered() {
38+
return power;
39+
}
40+
41+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.language.programming.model;
2+
3+
public interface Service {
4+
void shutdown(Client client);
5+
6+
String getInfo(Client client);
7+
8+
boolean isPowered();
9+
10+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.language.programming.model;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
import static org.junit.jupiter.api.Assertions.assertFalse;
7+
import static org.hamcrest.MatcherAssert.assertThat;
8+
import static org.hamcrest.Matchers.*;
9+
import org.junit.jupiter.api.BeforeAll;
10+
11+
public class ProxyTest {
12+
13+
static Proxy proxy;
14+
15+
@BeforeAll
16+
static void setUp() {
17+
proxy = new Proxy("192.168.100.200");
18+
}
19+
20+
@Test
21+
public void shouldReturnServerInfo() {
22+
Client client = new Client("user", Role.ADMIN);
23+
24+
assertThat(proxy.getInfo(client),
25+
matchesPattern("ipAddress: (.*)" + "\nMemTotal: (\\d*)kB"
26+
+ "\nMemAvailable: (\\d*)kB" + "\ncpuUsage: (\\d*)%"
27+
+ "\nDiskUsage: (\\d*)%"));
28+
}
29+
30+
@Test
31+
public void shouldShutdownServer() {
32+
Client client = new Client("user", Role.ADMIN);
33+
proxy.shutdown(client);
34+
assertFalse(proxy.isPowered());
35+
}
36+
37+
@Test
38+
public void shouldFailShutdownServer() {
39+
Client client = new Client("user", Role.USER);
40+
41+
assertThrows(IllegalAccessError.class, () -> {
42+
proxy.shutdown(client);
43+
});
44+
}
45+
46+
}

0 commit comments

Comments
 (0)