-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
198 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,7 @@ | ||
# ☕ Java Design Patterns | ||
|
||
## About | ||
## Proxy | ||
|
||
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 | ||
each week, and they are divided in different [branches](https://github.com/LBeghini/Java-Design-Patterns/branches). | ||
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. | ||
|
||
The main branch is just a template for every other branch. | ||
|
||
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. | ||
|
||
## Implemented design patterns | ||
|
||
### Behavioural patterns | ||
|
||
- [x] [Chain of responsibility](https://github.com/LBeghini/Java-Design-Patterns/tree/4-chain-of-responsibility) | ||
- [x] [Command](https://github.com/LBeghini/Java-Design-Patterns/tree/6-command) | ||
- [x] [Iterator](https://github.com/LBeghini/Java-Design-Patterns/tree/4-iterator) | ||
- [x] [Memento](https://github.com/LBeghini/Java-Design-Patterns/tree/5-memento) | ||
- [x] [Observer](https://github.com/LBeghini/Java-Design-Patterns/tree/5-observer) | ||
- [x] [State](https://github.com/LBeghini/Java-Design-Patterns/tree/3-state) | ||
- [x] [Strategy](https://github.com/LBeghini/Java-Design-Patterns/tree/6-strategy) | ||
- [x] [Template method](https://github.com/LBeghini/Java-Design-Patterns/tree/4-template-method) | ||
- [x] [Mediator](https://github.com/LBeghini/Java-Design-Patterns/tree/9-mediator) | ||
- [x] [Interpreter](https://github.com/LBeghini/Java-Design-Patterns/tree/9-interpreter) | ||
|
||
### Creational patterns | ||
|
||
- [ ] Abstract factory | ||
- [x] [Builder](https://github.com/LBeghini/Java-Design-Patterns/tree/1-builder) | ||
- [x] [Factory method](https://github.com/LBeghini/Java-Design-Patterns/tree/2-factory-method) | ||
- [x] [Prototype](https://github.com/LBeghini/Java-Design-Patterns/tree/2-prototype) | ||
- [x] [Singleton](https://github.com/LBeghini/Java-Design-Patterns/tree/1-singleton) | ||
|
||
### Structural patterns | ||
|
||
- [x] [Adapter](https://github.com/LBeghini/Java-Design-Patterns/tree/7-adapter) | ||
- [x] [Bridge](https://github.com/LBeghini/Java-Design-Patterns/tree/7-bridge) | ||
- [x] [Composite](https://github.com/LBeghini/Java-Design-Patterns/tree/8-composite) | ||
- [ ] Decorator | ||
- [x] [Facade](https://github.com/LBeghini/Java-Design-Patterns/tree/8-facade) | ||
- [ ] Flyweight | ||
- [ ] Proxy | ||
|
||
## Technologies | ||
|
||
- Java | ||
- JUnit | ||
- Maven | ||
|
||
## Requirements | ||
|
||
To run and edit the project, be sure to have installed in your computer the following softwares: | ||
- A code editor | ||
|
||
After that, you'll need to clone this repo: | ||
|
||
```bash | ||
git clone https://github.com/LBeghini/Java-Design-Patterns.git | ||
``` | ||
|
||
## Change branch | ||
|
||
To change to a different branch, run the command: | ||
|
||
```bash | ||
git checkout name-of-the-branch | ||
``` | ||
|
||
The branch names have the pattern: | ||
|
||
```bash | ||
{number-of-the-week}-{pattern-name} | ||
``` | ||
|
||
> `number-of-the-week` corresponds to the week asked to be implemented certain pattern | ||
## Testing | ||
|
||
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 | ||
of the patterns. | ||
|
||
You can run the tests using the maven wrapper: | ||
|
||
```bash | ||
./mvnw test | ||
``` | ||
|
||
## :balance_scale: License | ||
|
||
[MIT License](https://github.com/LBeghini/Java-Design-Patterns/blob/main/LICENSE) | ||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.language.programming.model; | ||
|
||
public class Client { | ||
|
||
private String username; | ||
private Role role; | ||
|
||
public Client(String username, Role role) { | ||
this.username = username; | ||
this.role = role; | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
|
||
public Role getRole() { | ||
return role; | ||
} | ||
|
||
public void setRole(Role role) { | ||
this.role = role; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.language.programming.model; | ||
|
||
public class Proxy implements Service { | ||
|
||
private Server server; | ||
private String ip; | ||
|
||
public Proxy(String ip) { | ||
this.ip = ip; | ||
} | ||
|
||
@Override | ||
public void shutdown(Client client) { | ||
|
||
if (!checkAccess(client)) { | ||
throw new IllegalAccessError("Unauthorized."); | ||
} | ||
|
||
if (this.server == null) { | ||
this.server = new Server(this.ip); | ||
} | ||
|
||
this.server.shutdown(client); | ||
} | ||
|
||
@Override | ||
public String getInfo(Client client) { | ||
|
||
if (this.server == null) { | ||
this.server = new Server(this.ip); | ||
} | ||
return this.server.getInfo(client); | ||
} | ||
|
||
public boolean checkAccess(Client client) { | ||
if (!client.getRole().equals(Role.ADMIN)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
|
||
} | ||
|
||
@Override | ||
public boolean isPowered() { | ||
|
||
if (this.server == null) { | ||
this.server = new Server(this.ip); | ||
} | ||
|
||
return this.server.isPowered(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.language.programming.model; | ||
|
||
public enum Role { | ||
ADMIN, USER; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.language.programming.model; | ||
|
||
import java.util.Random; | ||
|
||
public class Server implements Service { | ||
|
||
private String ip; | ||
private Integer memory; | ||
private Integer CPU; | ||
private Integer disk; | ||
private boolean power; | ||
|
||
public Server(String ip) { | ||
Random random = new Random(); | ||
|
||
this.ip = ip; | ||
|
||
this.memory = random.nextInt(8000); | ||
this.CPU = random.nextInt(100); | ||
this.disk = random.nextInt(100); | ||
this.power = true; | ||
|
||
} | ||
|
||
@Override | ||
public void shutdown(Client client) { | ||
this.power = false; | ||
} | ||
|
||
@Override | ||
public String getInfo(Client client) { | ||
return "ipAddress: " + this.ip + "\nMemTotal: " + 8000 + "kB" | ||
+ "\nMemAvailable: " + (8000 - this.memory) + "kB" + "\ncpuUsage: " | ||
+ this.CPU + "%" + "\nDiskUsage: " + this.disk + "%"; | ||
} | ||
|
||
public boolean isPowered() { | ||
return power; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.language.programming.model; | ||
|
||
public interface Service { | ||
void shutdown(Client client); | ||
|
||
String getInfo(Client client); | ||
|
||
boolean isPowered(); | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
src/test/java/com/language/programming/model/ProxyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.language.programming.model; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.*; | ||
import org.junit.jupiter.api.BeforeAll; | ||
|
||
public class ProxyTest { | ||
|
||
static Proxy proxy; | ||
|
||
@BeforeAll | ||
static void setUp() { | ||
proxy = new Proxy("192.168.100.200"); | ||
} | ||
|
||
@Test | ||
public void shouldReturnServerInfo() { | ||
Client client = new Client("user", Role.ADMIN); | ||
|
||
assertThat(proxy.getInfo(client), | ||
matchesPattern("ipAddress: (.*)" + "\nMemTotal: (\\d*)kB" | ||
+ "\nMemAvailable: (\\d*)kB" + "\ncpuUsage: (\\d*)%" | ||
+ "\nDiskUsage: (\\d*)%")); | ||
} | ||
|
||
@Test | ||
public void shouldShutdownServer() { | ||
Client client = new Client("user", Role.ADMIN); | ||
proxy.shutdown(client); | ||
assertFalse(proxy.isPowered()); | ||
} | ||
|
||
@Test | ||
public void shouldFailShutdownServer() { | ||
Client client = new Client("user", Role.USER); | ||
|
||
assertThrows(IllegalAccessError.class, () -> { | ||
proxy.shutdown(client); | ||
}); | ||
} | ||
|
||
} |