Add all 23 GoF design pattern implementations

This commit is contained in:
2026-07-25 10:50:29 +05:30
commit f5688a6b32
164 changed files with 4371 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
package interpreter;
/**
* NonTerminalExpression — addition: left + right.
*/
public class AddExpression implements Expression {
private final Expression left;
private final Expression right;
public AddExpression(Expression left, Expression right) {
this.left = left;
this.right = right;
}
@Override
public int interpret() {
return left.interpret() + right.interpret();
}
}

View File

@@ -0,0 +1,9 @@
package interpreter;
/**
* AbstractExpression — declares the interpret operation.
* All terminal and non-terminal expressions implement this.
*/
public interface Expression {
int interpret();
}

View File

@@ -0,0 +1,38 @@
package interpreter;
public class Main {
public static void main(String[] args) {
// (5 + 3) * 2 → 16
Expression expr1 = new MultiplyExpression(
new AddExpression(
new NumberExpression(5),
new NumberExpression(3)
),
new NumberExpression(2)
);
System.out.println("(5 + 3) * 2 = " + expr1.interpret());
// 10 - (4 + 2) → 4
Expression expr2 = new SubtractExpression(
new NumberExpression(10),
new AddExpression(
new NumberExpression(4),
new NumberExpression(2)
)
);
System.out.println("10 - (4 + 2) = " + expr2.interpret());
// (3 * 4) + (10 - 6) → 16
Expression expr3 = new AddExpression(
new MultiplyExpression(
new NumberExpression(3),
new NumberExpression(4)
),
new SubtractExpression(
new NumberExpression(10),
new NumberExpression(6)
)
);
System.out.println("(3 * 4) + (10 - 6) = " + expr3.interpret());
}
}

View File

@@ -0,0 +1,19 @@
package interpreter;
/**
* NonTerminalExpression — multiplication: left * right.
*/
public class MultiplyExpression implements Expression {
private final Expression left;
private final Expression right;
public MultiplyExpression(Expression left, Expression right) {
this.left = left;
this.right = right;
}
@Override
public int interpret() {
return left.interpret() * right.interpret();
}
}

View File

@@ -0,0 +1,18 @@
package interpreter;
/**
* TerminalExpression — a number literal.
* Leaf node in the AST; has no child expressions.
*/
public class NumberExpression implements Expression {
private final int number;
public NumberExpression(int number) {
this.number = number;
}
@Override
public int interpret() {
return number;
}
}

View File

@@ -0,0 +1,31 @@
# Interpreter Design Pattern — Java Example
**Pattern:** Behavioral → Interpreter
**Article:** https://ankurm.com/interpreter-design-pattern-java/
## What this example shows
A math expression evaluator built as an abstract syntax tree. `Expression` declares `interpret()` — every AST node implements it. `NumberExpression` is the only terminal: a leaf with no children that returns its value directly. `AddExpression`, `SubtractExpression`, and `MultiplyExpression` are non-terminals: each holds two child expressions and applies its operator after recursively interpreting them. `Main` is the client — it assembles three small trees by hand (`(5 + 3) * 2`, `10 - (4 + 2)`, `(3 * 4) + (10 - 6)`) and evaluates each by calling `interpret()` on the root.
## How to run
```bash
javac interpreter/*.java -d out/interpreter
java -cp out/interpreter interpreter.Main
```
Requires Java 25.
## Post Section ↔ File Mapping
| Post Section | File(s) |
|---|---|
| Implementation: Math Expression Evaluator — the AbstractExpression | `Expression.java` |
| Implementation: Math Expression Evaluator — the TerminalExpression | `NumberExpression.java` |
| Implementation: Math Expression Evaluator — AddExpression | `AddExpression.java` |
| Implementation: Math Expression Evaluator — SubtractExpression | `SubtractExpression.java` |
| Implementation: Math Expression Evaluator — MultiplyExpression | `MultiplyExpression.java` |
| Implementation: Math Expression Evaluator — wiring it together | `Main.java` |
Article: https://ankurm.com/interpreter-design-pattern-java/
All patterns: https://ankurm.com/design-patterns-java/

View File

@@ -0,0 +1,19 @@
package interpreter;
/**
* NonTerminalExpression — subtraction: left - right.
*/
public class SubtractExpression implements Expression {
private final Expression left;
private final Expression right;
public SubtractExpression(Expression left, Expression right) {
this.left = left;
this.right = right;
}
@Override
public int interpret() {
return left.interpret() - right.interpret();
}
}