Skip to content

Commit bc791ba

Browse files
committed
Implements interpreter
1 parent 4d5a1ae commit bc791ba

File tree

7 files changed

+157
-86
lines changed

7 files changed

+157
-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+
## Interpreter
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+
Interpreter pattern interprets sentences to match an desired expression type.
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 we want to validate logical expressions such as greather than, less than and equals. For that, we would write "6 > 3" or "3 < 7" or "2 = 2". However, those are strings, and they must be interpreted so the computer can understand.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.language.programming.model;
2+
3+
public abstract class Interpreter {
4+
5+
protected Integer left;
6+
protected Integer right;
7+
8+
public Interpreter(Integer left, Integer right) {
9+
this.left = left;
10+
this.right = right;
11+
};
12+
13+
public abstract boolean interpret();
14+
15+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.language.programming.model;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import com.language.programming.model.operators.*;
7+
8+
public class InterpreterLogicExpressions {
9+
10+
private String expression;
11+
12+
public InterpreterLogicExpressions(String expression) {
13+
this.expression = expression;
14+
}
15+
16+
public boolean interpret() {
17+
List<String> elements = Arrays.asList(expression.split(" "));
18+
Integer left = 0;
19+
Integer right = 0;
20+
Interpreter interpreter = null;
21+
22+
if (elements.size() != 3) {
23+
throw new IllegalArgumentException();
24+
}
25+
26+
try {
27+
left = Integer.parseInt(elements.get(0));
28+
right = Integer.parseInt(elements.get(2));
29+
30+
} catch (Exception e) {
31+
e.printStackTrace();
32+
}
33+
34+
if (elements.get(1).equals(">")) {
35+
interpreter = new GreaterThan(left, right);
36+
}
37+
38+
if (elements.get(1).equals("<")) {
39+
interpreter = new LessThan(left, right);
40+
}
41+
42+
if (elements.get(1).equals("=")) {
43+
interpreter = new Equals(left, right);
44+
}
45+
46+
return interpreter.interpret();
47+
}
48+
49+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.language.programming.model.operators;
2+
3+
import com.language.programming.model.Interpreter;
4+
5+
public class Equals extends Interpreter {
6+
7+
public Equals(Integer left, Integer right) {
8+
super(left, right);
9+
}
10+
11+
@Override
12+
public boolean interpret() {
13+
return left == right;
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.language.programming.model.operators;
2+
3+
import com.language.programming.model.Interpreter;
4+
5+
public class GreaterThan extends Interpreter {
6+
7+
public GreaterThan(Integer left, Integer right) {
8+
super(left, right);
9+
}
10+
11+
@Override
12+
public boolean interpret() {
13+
return left > right;
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.language.programming.model.operators;
2+
3+
import com.language.programming.model.Interpreter;
4+
5+
public class LessThan extends Interpreter {
6+
7+
public LessThan(Integer left, Integer right) {
8+
super(left, right);
9+
}
10+
11+
@Override
12+
public boolean interpret() {
13+
return left < right;
14+
}
15+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 InterpreterTest {
8+
9+
@Test
10+
public void shouldReturnTrueGreatherThan() {
11+
InterpreterLogicExpressions interpreter = new InterpreterLogicExpressions("6 > 3");
12+
assertTrue(interpreter.interpret());
13+
}
14+
15+
@Test
16+
public void shouldReturnFalseGreatherThan() {
17+
InterpreterLogicExpressions interpreter = new InterpreterLogicExpressions("3 > 3");
18+
assertFalse(interpreter.interpret());
19+
}
20+
21+
@Test
22+
public void shouldReturnTrueLessThan() {
23+
InterpreterLogicExpressions interpreter = new InterpreterLogicExpressions("6 < 7");
24+
assertTrue(interpreter.interpret());
25+
}
26+
27+
@Test
28+
public void shouldReturnFalseLessThan() {
29+
InterpreterLogicExpressions interpreter = new InterpreterLogicExpressions("3 < 3");
30+
assertFalse(interpreter.interpret());
31+
}
32+
33+
@Test
34+
public void shouldReturnTrueEquals() {
35+
InterpreterLogicExpressions interpreter = new InterpreterLogicExpressions("6 = 6");
36+
assertTrue(interpreter.interpret());
37+
}
38+
39+
@Test
40+
public void shouldReturnFalseEquals() {
41+
InterpreterLogicExpressions interpreter = new InterpreterLogicExpressions("3 = 6");
42+
assertFalse(interpreter.interpret());
43+
}
44+
45+
}

0 commit comments

Comments
 (0)