====== Výrazy - Composite, Interpreter, Visitor ======
===== Expr.java =====
Představuje libovolný výraz
package vyrazy;
import java.io.IOException;
abstract public class Expr {
abstract double eval();
@Override
public abstract String toString();
public abstract void exportUsing(Exporter exporter) throws IOException;
}
===== Value.java =====
Představuje konstantní hodnotu
package vyrazy;
import java.io.IOException;
public class Value extends Expr{
double x;
public Value(double x){
this.x = x;
}
@Override
double eval() {
return x;
}
@Override
public String toString() {
return String.valueOf(x);
}
@Override
public void exportUsing(Exporter exporter) throws IOException {
exporter.exportValue(this);
}
}
===== BinOp.java =====
Společná nadtřída binárních operací
package vyrazy;
abstract public class BinOp extends Expr{
Expr op1;
Expr op2;
public BinOp(Expr x, Expr y){
op1 = x;
op2 = y;
}
}
===== Plus.java =====
package vyrazy;
import java.io.IOException;
public class Plus extends BinOp{
public Plus(Expr x, Expr y){
super(x,y);
}
@Override
double eval() {
return op1.eval() + op2.eval();
}
@Override
public String toString() {
return op1.toString() + " + " + op2.toString();
}
@Override
public void exportUsing(Exporter exporter) throws IOException {
exporter.exportPlus(this);
}
}
===== Multiply.java =====
package vyrazy;
import java.io.IOException;
public class Multiply extends BinOp{
public Multiply(Expr x, Expr y){
super(x,y);
}
@Override
double eval() {
return op1.eval() * op2.eval();
}
@Override
public String toString() {
return op1.toString() + " * " + op2.toString();
}
@Override
public void exportUsing(Exporter exporter) throws IOException {
exporter.exportMultiply(this);
}
}
===== Exporter.java =====
Definuje společný kód a rozhraní exportéru do textového souboru.
package vyrazy;
import java.io.IOException;
import java.io.Writer;
public abstract class Exporter {
Writer writer;
public Exporter(Writer writer) {
this.writer = writer;
}
abstract void exportValue(Value expr) throws IOException;
abstract void exportPlus(Plus expr) throws IOException;
abstract void exportMultiply(Multiply expr) throws IOException;
void exportExpr(Expr expr) throws IOException {
expr.exportUsing(this);
}
}
===== TextExporter.java =====
Konkrétní implementace exportéru, v infixové notaci.
package vyrazy;
import java.io.IOException;
import java.io.Writer;
public class TextExporter extends Exporter {
public TextExporter(Writer writer) {
super(writer);
}
void exportValue(Value expr) throws IOException {
writer.write(String.valueOf(expr.x));
}
@Override
void exportPlus(Plus expr) throws IOException {
expr.op1.exportUsing(this);
writer.write(" + ");
expr.op2.exportUsing(this);
}
void exportMultiply(Multiply expr) throws IOException {
expr.op1.exportUsing(this);
writer.write(" * ");
expr.op2.exportUsing(this);
}
}
===== PostfixTextExporter.java =====
Exportér v postfixové notaci.
package vyrazy;
import java.io.IOException;
import java.io.Writer;
public class PostfixTextExporter extends Exporter {
public PostfixTextExporter(Writer writer) {
super(writer);
}
void exportValue(Value expr) throws IOException {
writer.write(String.valueOf(expr.x));
}
@Override
void exportPlus(Plus expr) throws IOException {
expr.op1.exportUsing(this);
writer.write(" ");
expr.op2.exportUsing(this);
writer.write(" +");
}
@Override
void exportMultiply(Multiply expr) throws IOException {
expr.op1.exportUsing(this);
writer.write(" ");
expr.op2.exportUsing(this);
writer.write(" *");
}
}
===== Main.java =====
package vyrazy;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
public class Main {
public static void main(String[] args) throws IOException {
Expr vyraz = new Multiply (new Value(1), new Value(3));
vyraz = new Plus (vyraz, new Value(4));
/* soubor emulujeme StringWriterem */
Writer w = new StringWriter();
Exporter ex = new TextExporter(w);
ex.exportExpr(vyraz);
System.out.println(w);
}
}