Skip to content

Commit 1d67a9c

Browse files
Merge pull request #24 from darbyluv2code/feature/add-interpreter-pattern
Add Interpreter Pattern
2 parents 1a3b7d5 + 5f50176 commit 1d67a9c

File tree

9 files changed

+216
-0
lines changed

9 files changed

+216
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.luv2code</groupId>
8+
<artifactId>java-design-patterns</artifactId>
9+
<version>1.0</version>
10+
11+
<properties>
12+
<maven.compiler.source>25</maven.compiler.source>
13+
<maven.compiler.target>25</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.luv2code.designpatterns.behavioral.interpreter;
2+
3+
/**
4+
* Role: Context
5+
*
6+
* Holds the shared state passed to every expression during interpretation.
7+
*/
8+
public class ChatContext {
9+
10+
private String currentUser;
11+
12+
public ChatContext(String currentUser) {
13+
this.currentUser = currentUser;
14+
}
15+
16+
public String getCurrentUser() {
17+
return currentUser;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.luv2code.designpatterns.behavioral.interpreter;
2+
3+
/**
4+
* Role: Helper (Parser)
5+
*
6+
* Parses a raw command string and builds the appropriate Expression.
7+
*
8+
* Not part of the GoF pattern itself ... this is the caller that constructs
9+
* and triggers the expressions.
10+
*/
11+
public class CommandParser {
12+
13+
public Expression parse(String inputCommand) {
14+
15+
if (inputCommand == null || inputCommand.isBlank()) {
16+
return new InvalidCommandExpression("empty input");
17+
}
18+
19+
String trimmedInput = inputCommand.trim();
20+
String[] parts = trimmedInput.split("\\s+", 2);
21+
22+
String commandName = parts[0];
23+
String argument = parts.length > 1 ? parts[1] : "";
24+
25+
return switch (commandName) {
26+
case "/join" -> new JoinExpression(argument);
27+
case "/mute" -> new MuteExpression(argument);
28+
case "/remind" -> new RemindExpression(argument);
29+
default -> new InvalidCommandExpression(commandName);
30+
};
31+
}
32+
}
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.luv2code.designpatterns.behavioral.interpreter;
2+
3+
/**
4+
* Role: Abstract Expression
5+
*
6+
* Declares the interpret() method that all expressions must implement.
7+
*/
8+
public interface Expression {
9+
10+
void interpret(ChatContext chatContext);
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.luv2code.designpatterns.behavioral.interpreter;
2+
3+
/**
4+
* Role: Concrete / Terminal Expression
5+
*
6+
* Handles unrecognized commands by reporting an error.
7+
*/
8+
public class InvalidCommandExpression implements Expression {
9+
10+
private String commandText;
11+
12+
public InvalidCommandExpression(String commandText) {
13+
this.commandText = commandText;
14+
}
15+
16+
@Override
17+
public void interpret(ChatContext chatContext) {
18+
System.out.println("[Error] Invalid command: " + commandText);
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.luv2code.designpatterns.behavioral.interpreter;
2+
3+
/**
4+
* Role: Concrete / Terminal Expression
5+
*
6+
* Handles the /join command by adding the user to a chat room.
7+
*/
8+
public class JoinExpression implements Expression{
9+
10+
private String roomName;
11+
12+
public JoinExpression(String roomName) {
13+
this.roomName = roomName;
14+
}
15+
16+
@Override
17+
public void interpret(ChatContext chatContext) {
18+
System.out.println("[Join] " + chatContext.getCurrentUser()
19+
+ " joined room: " + roomName);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.luv2code.designpatterns.behavioral.interpreter;
2+
3+
/**
4+
* Role: Client
5+
*
6+
* Drives the demo by sending sample chat commands through the interpreter.
7+
*/
8+
public class MainApp {
9+
10+
static void main() {
11+
12+
System.out.println("--- Demo: Chat Command Interpreter ---");
13+
14+
CommandParser commandParser = new CommandParser();
15+
16+
runCommand("/join java-room", "Angel", commandParser);
17+
runCommand("/mute Liam", "Angel", commandParser);
18+
runCommand("/remind Team meeting at 3pm", "Angel", commandParser);
19+
runCommand("/dance now", "Angel", commandParser);
20+
21+
}
22+
23+
private static void runCommand(String inputCommand,
24+
String currentUser,
25+
CommandParser commandParser) {
26+
27+
ChatContext chatContext = new ChatContext(currentUser);
28+
29+
System.out.println("\nInput: " + inputCommand);
30+
31+
Expression parsedExpression = commandParser.parse(inputCommand);
32+
parsedExpression.interpret(chatContext);
33+
}
34+
}
35+
36+
37+
38+
39+
40+
41+
42+
43+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.luv2code.designpatterns.behavioral.interpreter;
2+
3+
/**
4+
* Role: Concrete / Terminal Expression
5+
*
6+
* Handles the /mute command by silencing a target user.
7+
*/
8+
public class MuteExpression implements Expression {
9+
10+
private String targetUser;
11+
12+
public MuteExpression(String targetUser) {
13+
this.targetUser = targetUser;
14+
}
15+
16+
@Override
17+
public void interpret(ChatContext chatContext) {
18+
System.out.println("[Mute] " + chatContext.getCurrentUser()
19+
+ " muted user: " + targetUser);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.luv2code.designpatterns.behavioral.interpreter;
2+
3+
/**
4+
* Role: Concrete / Terminal Expression
5+
*
6+
* Handles the /remind command by creating a reminder for the current user.
7+
*/
8+
public class RemindExpression implements Expression {
9+
10+
private String reminderText;
11+
12+
public RemindExpression(String reminderText) {
13+
this.reminderText = reminderText;
14+
}
15+
16+
@Override
17+
public void interpret(ChatContext chatContext) {
18+
System.out.println("[Remind] Reminder created for "
19+
+ chatContext.getCurrentUser() + ": " + reminderText);
20+
}
21+
}

0 commit comments

Comments
 (0)