Skip to content

Commit

Permalink
Implements proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
LBeghini committed Aug 16, 2021
1 parent 4504cfd commit 78f107a
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 88 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,6 @@ buildNumber.properties
.history
.ionide

# End of https://www.toptal.com/developers/gitignore/api/java,maven,visualstudiocode
# End of https://www.toptal.com/developers/gitignore/api/java,maven,visualstudiocode

.vscode
90 changes: 3 additions & 87 deletions README.md
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.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
<version>5.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
<version>2.0.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/language/programming/model/Client.java
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;
}

}
54 changes: 54 additions & 0 deletions src/main/java/com/language/programming/model/Proxy.java
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();
}

}
6 changes: 6 additions & 0 deletions src/main/java/com/language/programming/model/Role.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.language.programming.model;

public enum Role {
ADMIN, USER;

}
41 changes: 41 additions & 0 deletions src/main/java/com/language/programming/model/Server.java
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;
}

}
10 changes: 10 additions & 0 deletions src/main/java/com/language/programming/model/Service.java
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 src/test/java/com/language/programming/model/ProxyTest.java
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);
});
}

}

0 comments on commit 78f107a

Please sign in to comment.