Reformat rest of the design patterns - Issue #224
This commit is contained in:
@ -4,75 +4,66 @@ import java.util.Stack;
|
||||
|
||||
/**
|
||||
*
|
||||
* The Interpreter pattern is a design pattern that specifies how to evaluate sentences
|
||||
* in a language. The basic idea is to have a class for each symbol (terminal or nonterminal)
|
||||
* in a specialized computer language. The syntax tree of a sentence in the language is an
|
||||
* instance of the composite pattern and is used to evaluate (interpret) the sentence for a
|
||||
* client.
|
||||
* The Interpreter pattern is a design pattern that specifies how to evaluate sentences in a
|
||||
* language. The basic idea is to have a class for each symbol (terminal or nonterminal) in a
|
||||
* specialized computer language. The syntax tree of a sentence in the language is an instance of
|
||||
* the composite pattern and is used to evaluate (interpret) the sentence for a client.
|
||||
* <p>
|
||||
* In this example we use the Interpreter pattern to break sentences into expressions
|
||||
* ({@link Expression}) that can be evaluated and as a whole form the result.
|
||||
* In this example we use the Interpreter pattern to break sentences into expressions (
|
||||
* {@link Expression}) that can be evaluated and as a whole form the result.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
*
|
||||
* Program entry point.
|
||||
* <p>
|
||||
* Expressions can be evaluated using prefix, infix or postfix notations
|
||||
* This sample uses postfix, where operator comes after the operands
|
||||
*
|
||||
* @param args command line args
|
||||
*
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
String tokenString = "4 3 2 - 1 + *";
|
||||
Stack<Expression> stack = new Stack<>();
|
||||
/**
|
||||
*
|
||||
* Program entry point.
|
||||
* <p>
|
||||
* Expressions can be evaluated using prefix, infix or postfix notations This sample uses postfix,
|
||||
* where operator comes after the operands
|
||||
*
|
||||
* @param args command line args
|
||||
*
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
String tokenString = "4 3 2 - 1 + *";
|
||||
Stack<Expression> stack = new Stack<>();
|
||||
|
||||
String[] tokenList = tokenString.split(" ");
|
||||
for (String s : tokenList) {
|
||||
if (isOperator(s)) {
|
||||
Expression rightExpression = stack.pop();
|
||||
Expression leftExpression = stack.pop();
|
||||
System.out
|
||||
.println(String.format(
|
||||
"popped from stack left: %d right: %d",
|
||||
leftExpression.interpret(),
|
||||
rightExpression.interpret()));
|
||||
Expression operator = getOperatorInstance(s, leftExpression,
|
||||
rightExpression);
|
||||
System.out.println(String.format("operator: %s", operator));
|
||||
int result = operator.interpret();
|
||||
NumberExpression resultExpression = new NumberExpression(result);
|
||||
stack.push(resultExpression);
|
||||
System.out.println(String.format("push result to stack: %d",
|
||||
resultExpression.interpret()));
|
||||
} else {
|
||||
Expression i = new NumberExpression(s);
|
||||
stack.push(i);
|
||||
System.out.println(String.format("push to stack: %d",
|
||||
i.interpret()));
|
||||
}
|
||||
}
|
||||
System.out
|
||||
.println(String.format("result: %d", stack.pop().interpret()));
|
||||
}
|
||||
String[] tokenList = tokenString.split(" ");
|
||||
for (String s : tokenList) {
|
||||
if (isOperator(s)) {
|
||||
Expression rightExpression = stack.pop();
|
||||
Expression leftExpression = stack.pop();
|
||||
System.out.println(String.format("popped from stack left: %d right: %d",
|
||||
leftExpression.interpret(), rightExpression.interpret()));
|
||||
Expression operator = getOperatorInstance(s, leftExpression, rightExpression);
|
||||
System.out.println(String.format("operator: %s", operator));
|
||||
int result = operator.interpret();
|
||||
NumberExpression resultExpression = new NumberExpression(result);
|
||||
stack.push(resultExpression);
|
||||
System.out.println(String.format("push result to stack: %d", resultExpression.interpret()));
|
||||
} else {
|
||||
Expression i = new NumberExpression(s);
|
||||
stack.push(i);
|
||||
System.out.println(String.format("push to stack: %d", i.interpret()));
|
||||
}
|
||||
}
|
||||
System.out.println(String.format("result: %d", stack.pop().interpret()));
|
||||
}
|
||||
|
||||
public static boolean isOperator(String s) {
|
||||
return s.equals("+") || s.equals("-") || s.equals("*");
|
||||
}
|
||||
public static boolean isOperator(String s) {
|
||||
return s.equals("+") || s.equals("-") || s.equals("*");
|
||||
}
|
||||
|
||||
public static Expression getOperatorInstance(String s, Expression left,
|
||||
Expression right) {
|
||||
switch (s) {
|
||||
case "+":
|
||||
return new PlusExpression(left, right);
|
||||
case "-":
|
||||
return new MinusExpression(left, right);
|
||||
case "*":
|
||||
return new MultiplyExpression(left, right);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public static Expression getOperatorInstance(String s, Expression left, Expression right) {
|
||||
switch (s) {
|
||||
case "+":
|
||||
return new PlusExpression(left, right);
|
||||
case "-":
|
||||
return new MinusExpression(left, right);
|
||||
case "*":
|
||||
return new MultiplyExpression(left, right);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -7,8 +7,8 @@ package com.iluwatar.interpreter;
|
||||
*/
|
||||
public abstract class Expression {
|
||||
|
||||
public abstract int interpret();
|
||||
public abstract int interpret();
|
||||
|
||||
@Override
|
||||
public abstract String toString();
|
||||
@Override
|
||||
public abstract String toString();
|
||||
}
|
||||
|
@ -7,22 +7,22 @@ package com.iluwatar.interpreter;
|
||||
*/
|
||||
public class MinusExpression extends Expression {
|
||||
|
||||
private Expression leftExpression;
|
||||
private Expression rightExpression;
|
||||
private Expression leftExpression;
|
||||
private Expression rightExpression;
|
||||
|
||||
public MinusExpression(Expression leftExpression, Expression rightExpression) {
|
||||
this.leftExpression = leftExpression;
|
||||
this.rightExpression = rightExpression;
|
||||
}
|
||||
public MinusExpression(Expression leftExpression, Expression rightExpression) {
|
||||
this.leftExpression = leftExpression;
|
||||
this.rightExpression = rightExpression;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int interpret() {
|
||||
return leftExpression.interpret() - rightExpression.interpret();
|
||||
}
|
||||
@Override
|
||||
public int interpret() {
|
||||
return leftExpression.interpret() - rightExpression.interpret();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "-";
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "-";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,23 +7,22 @@ package com.iluwatar.interpreter;
|
||||
*/
|
||||
public class MultiplyExpression extends Expression {
|
||||
|
||||
private Expression leftExpression;
|
||||
private Expression rightExpression;
|
||||
private Expression leftExpression;
|
||||
private Expression rightExpression;
|
||||
|
||||
public MultiplyExpression(Expression leftExpression,
|
||||
Expression rightExpression) {
|
||||
this.leftExpression = leftExpression;
|
||||
this.rightExpression = rightExpression;
|
||||
}
|
||||
public MultiplyExpression(Expression leftExpression, Expression rightExpression) {
|
||||
this.leftExpression = leftExpression;
|
||||
this.rightExpression = rightExpression;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int interpret() {
|
||||
return leftExpression.interpret() * rightExpression.interpret();
|
||||
}
|
||||
@Override
|
||||
public int interpret() {
|
||||
return leftExpression.interpret() * rightExpression.interpret();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "*";
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "*";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,24 +7,23 @@ package com.iluwatar.interpreter;
|
||||
*/
|
||||
public class NumberExpression extends Expression {
|
||||
|
||||
private int number;
|
||||
private int number;
|
||||
|
||||
public NumberExpression(int number) {
|
||||
this.number = number;
|
||||
}
|
||||
public NumberExpression(int number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
public NumberExpression(String s) {
|
||||
this.number = Integer.parseInt(s);
|
||||
}
|
||||
public NumberExpression(String s) {
|
||||
this.number = Integer.parseInt(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int interpret() {
|
||||
return number;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "number";
|
||||
}
|
||||
@Override
|
||||
public int interpret() {
|
||||
return number;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "number";
|
||||
}
|
||||
}
|
||||
|
@ -7,22 +7,21 @@ package com.iluwatar.interpreter;
|
||||
*/
|
||||
public class PlusExpression extends Expression {
|
||||
|
||||
private Expression leftExpression;
|
||||
private Expression rightExpression;
|
||||
private Expression leftExpression;
|
||||
private Expression rightExpression;
|
||||
|
||||
public PlusExpression(Expression leftExpression, Expression rightExpression) {
|
||||
this.leftExpression = leftExpression;
|
||||
this.rightExpression = rightExpression;
|
||||
}
|
||||
public PlusExpression(Expression leftExpression, Expression rightExpression) {
|
||||
this.leftExpression = leftExpression;
|
||||
this.rightExpression = rightExpression;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int interpret() {
|
||||
return leftExpression.interpret() + rightExpression.interpret();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "+";
|
||||
}
|
||||
@Override
|
||||
public int interpret() {
|
||||
return leftExpression.interpret() + rightExpression.interpret();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "+";
|
||||
}
|
||||
}
|
||||
|
@ -11,9 +11,9 @@ import com.iluwatar.interpreter.App;
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user