Advertisement
Ladies_Man

Abstr. Expr. Factory

Nov 10th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.74 KB | None | 0 0
  1. public interface Expr<T>
  2. {
  3.         void accept(ExprVisitor<T> v);
  4. }
  5.  
  6. public interface Neg<T> extends Expr<T>
  7. {
  8.         Expr<T> a(); // операнд унарного минуса
  9. }
  10.  
  11. public interface Binary<T> extends Expr<T>
  12. {
  13.         Expr<T> a(); // первый операнд
  14.         Expr<T> b(); // второй операнд
  15.         char operation(); // ’+’, ’-’, ’*’, ’/’
  16. }
  17.  
  18. public interface Var<T> extends Expr<T>
  19. {
  20. }
  21.  
  22. public interface Const<T> extends Expr<T>
  23. {
  24.         T value(); // значение константы
  25. }
  26.  
  27. //========================================================================================
  28.  
  29. public interface ExprVisitor<T>
  30. {
  31.         void visitNeg(Neg<T> e);
  32.         void visitBinary(Binary<T> e);
  33.         void visitVar(Var<T> e);
  34.         void visitConst(Const<T> e);
  35. }
  36.  
  37. //========================================================================================
  38.  
  39. public interface AbstractExprFactory<T>
  40. {
  41.         Neg<T> newNeg(Expr<T> a);
  42.         Binary<T> newBinary(Expr<T> a, Expr<T> b, char operation);
  43.         Var<T> newVar();
  44.         Const<T> newConst(T value);
  45. }
  46.  
  47. //========================================================================================
  48.  
  49. public class ExprFactory<T> implements AbstractExprFactory<T> {
  50.         private class Neg1<T> implements Neg<T> {
  51.         private Expr<T> a1;
  52.         public Neg1 (Expr<T> a) {
  53.             this.a1 = a;
  54.         }
  55.         public Expr<T> a() {
  56.             return a1;
  57.         }
  58.         public void accept (ExprVisitor<T> v) {
  59.             v.visitNeg(this);
  60.         }
  61.     }
  62.    
  63.     private class Binary1<T> implements Binary<T> {
  64.         private Expr<T> a1;
  65.         private Expr<T> b1;
  66.         private char operation1;
  67.         public Binary1 (Expr<T> a, Expr<T> b, char operation) {
  68.             this.a1 = a;
  69.             this.b1 = b;
  70.             this.operation1 = operation;
  71.         }
  72.         public Expr<T> a() {
  73.             return a1;
  74.         }
  75.         public Expr<T> b() {
  76.             return b1;
  77.         }
  78.         public char operation() {
  79.             return operation1;
  80.         }
  81.         public void accept (ExprVisitor<T> v) {
  82.             v.visitBinary(this);
  83.         }
  84.     }
  85.    
  86.     private class Var1<T> implements Var<T> {
  87.         public void accept (ExprVisitor<T> v) {
  88.             v.visitVar(this);
  89.         }
  90.     }
  91.    
  92.     private class Const1<T> implements Const<T> {
  93.         private T value1;
  94.         public Const1 (T value) {
  95.             this.value1 = value;
  96.         }
  97.         public  T value () {
  98.             return value1;
  99.         }
  100.         public void accept (ExprVisitor<T> v) {
  101.             v.visitConst(this);
  102.         }
  103.     }
  104. //==================================================
  105.     public Neg<T> newNeg (Expr<T> a) {
  106.         return new Neg1<T>(a);
  107.     }
  108.  
  109.     public Binary<T> newBinary (Expr<T> a, Expr<T> b, char operation) {
  110.         return new Binary1<T>(a, b, operation);
  111.     }
  112.    
  113.     public Var<T> newVar () {
  114.         return new Var1<T>();
  115.     }
  116.  
  117.     public  Const<T> newConst (T value) {
  118.         return new Const1<T>(value);
  119.     }
  120. }
  121.  
  122. //========================================================================================
  123.  
  124. // CountVisitor предназначен для подсчёта количества
  125. // узлов разного типа в дереве арифметических выражений.
  126. class CountVisitor<T> implements ExprVisitor<T>
  127. {
  128.         private int negs, binaries, vars, consts;
  129.  
  130.         public CountVisitor(Expr<T> e)
  131.         {
  132.                 e.accept(this);
  133.         }
  134.  
  135.         public int negs() { return negs; }
  136.         public int binaries() { return binaries; }
  137.         public int vars() { return vars; }
  138.         public int consts() { return consts; }
  139.  
  140.         public void visitNeg(Neg<T> e)
  141.         {
  142.                 negs++;
  143.                 e.a().accept(this);
  144.         }
  145.  
  146.         public void visitBinary(Binary<T> e)
  147.         {
  148.                 binaries++;
  149.                 e.a().accept(this);
  150.                 e.b().accept(this);
  151.         }
  152.  
  153.         public void visitVar(Var<T> e) { vars++; }
  154.  
  155.         public void visitConst(Const<T> e) { consts++; }
  156. }
  157.  
  158. public class Test
  159. {
  160.         public static void main(String[] args)
  161.         {
  162.                 ExprFactory<Integer> f = new ExprFactory<Integer>();
  163.  
  164.                 Expr<Integer> e =
  165.                         f.newBinary(
  166.                                 f.newBinary(
  167.                                         f.newVar(),
  168.                                         f.newConst(10),
  169.                                         ’+
  170.                                 ),
  171.                                 f.newVar(),
  172.                                 ’*
  173.                         );
  174.  
  175.                 CountVisitor<Integer> v = new CountVisitor<Integer>(e);
  176.                 System.out.println(v.negs());
  177.                 System.out.println(v.binaries());
  178.                 System.out.println(v.vars());
  179.                 System.out.println(v.consts());
  180.         }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement