Skip to content

Commit 2bbd4af

Browse files
committed
Implements adapter
1 parent d7ad582 commit 2bbd4af

File tree

7 files changed

+161
-86
lines changed

7 files changed

+161
-86
lines changed

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+
## Adapter
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+
Adapter pattern converts the interface of a class into another interface as expected. Wrap an incompatible object in an adapter to make it compatible with another class.
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-
- [ ] 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-
- [ ] 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)
7+
For example, let's say you're at a Windows virtual machine running on top of a Linux host, using the command line. There is an Hypervisor in between that adapts your windows commands into unix commands, and you as the user does not even know it's doing it.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.language.programming.model;
2+
3+
public interface Command {
4+
5+
String getCommand();
6+
7+
void setCommand(String command);
8+
9+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.language.programming.model;
2+
3+
public class Hypervisor extends UnixCommand {
4+
5+
private Command windowsCommand;
6+
7+
public Hypervisor(Command windowsCommand) {
8+
this.windowsCommand = windowsCommand;
9+
}
10+
11+
public void executeCommand() {
12+
if (windowsCommand.getCommand().equals("dir")) {
13+
this.setCommand("ls");
14+
return;
15+
}
16+
17+
if (windowsCommand.getCommand().equals("cls")) {
18+
this.setCommand("clear");
19+
return;
20+
}
21+
22+
if (windowsCommand.getCommand().equals("copy")) {
23+
this.setCommand("cp");
24+
return;
25+
}
26+
27+
if (windowsCommand.getCommand().equals("find")) {
28+
this.setCommand("grep");
29+
return;
30+
}
31+
32+
if (windowsCommand.getCommand().equals("move")) {
33+
this.setCommand("mv");
34+
return;
35+
}
36+
37+
if (windowsCommand.getCommand().equals("del")) {
38+
this.setCommand("rm");
39+
return;
40+
}
41+
42+
}
43+
44+
public String recoverCommand() {
45+
46+
if (this.getCommand().equals("ls")) {
47+
windowsCommand.setCommand("dir");
48+
}
49+
50+
if (this.getCommand().equals("clear")) {
51+
windowsCommand.setCommand("cls");
52+
}
53+
54+
if (this.getCommand().equals("cp")) {
55+
windowsCommand.setCommand("copy");
56+
}
57+
58+
if (this.getCommand().equals("grep")) {
59+
windowsCommand.setCommand("find");
60+
}
61+
62+
if (this.getCommand().equals("mv")) {
63+
windowsCommand.setCommand("move");
64+
}
65+
66+
if (this.getCommand().equals("rm")) {
67+
windowsCommand.setCommand("del");
68+
}
69+
70+
return windowsCommand.getCommand();
71+
72+
}
73+
74+
}
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 class UnixCommand implements Command {
4+
5+
private String command;
6+
7+
@Override
8+
public String getCommand() {
9+
return this.command;
10+
}
11+
12+
@Override
13+
public void setCommand(String command) {
14+
this.command = command;
15+
16+
}
17+
18+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.language.programming.model;
2+
3+
public class User {
4+
5+
Command command;
6+
Hypervisor adapter;
7+
8+
public User() {
9+
command = new WindowsCommand();
10+
adapter = new Hypervisor(command);
11+
12+
}
13+
14+
public void setCommand(String windows) {
15+
command.setCommand(windows);
16+
adapter.executeCommand();
17+
}
18+
19+
public String getCommand() {
20+
return adapter.recoverCommand();
21+
}
22+
}
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 class WindowsCommand implements Command {
4+
5+
private String command;
6+
7+
@Override
8+
public String getCommand() {
9+
return this.command;
10+
}
11+
12+
@Override
13+
public void setCommand(String command) {
14+
this.command = command;
15+
16+
}
17+
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.language.programming.model;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
public class UserTest {
8+
9+
@Test
10+
public void shouldReturnWindowsCommand() {
11+
User user = new User();
12+
user.setCommand("dir");
13+
14+
assertEquals("dir", user.getCommand());
15+
}
16+
17+
}

0 commit comments

Comments
 (0)