SE450: A Linked List: Visitor [4/25] Previous pageContentsNext page

This is visitor.

A visitor is a double-dispatched iterator.

Visitors are typically internal iterators in which the iterator controls the traversal. (This is the most flexible form, as we will see when we get to trees.)

file:visitor/list/Main.java [source] [doc-public] [doc-private]
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package visitor.list;

/* public */
interface List {
  public <T> T accept(ListVisitor<T> v);
}

/* public */
class ListF {
  private ListF() {}
  public static final List nil = new Nil(); /* Singleton */
  public static final List cons(int hd, List tl) /* Factory */ {
    return new Cons(hd, tl);
  }
}

/* public */
interface ListVisitor<T> {
  public T visitNil();
  public T visitCons(int hd, List tl);
}

/*
 *************************************************************************
 * List classes.
 *************************************************************************
 */
class Nil implements List {
  Nil() {}
  public String toString() { return "nil"; }
  public <T> T accept(ListVisitor<T> v) {
    return v.visitNil();
  }
}

class Cons implements List {
  private final int hd;
  private final List tl;
  Cons(int hd, List tl) { this.hd = hd; this.tl = tl; }
  public String toString() { return hd + "::" + tl.toString(); }
  public <T> T accept(ListVisitor<T> v) {
    return v.visitCons(hd, tl);
  }
}

/*
 *************************************************************************
 * Visitor classes.
 * The visitor to a Cons is responsible for visiting the tl.
 *************************************************************************
 */
class Sum implements ListVisitor<Integer> {
  public Integer visitNil() { return 0; }
  public Integer visitCons(int hd, List tl) {
    return hd + tl.accept(this);
  }
}

class Reverse implements ListVisitor<List> {
  private List result = ListF.nil; // use a field to accumulate the value
  public List visitNil() { return result; }
  public List visitCons(int hd, List tl) {
    result = ListF.cons(hd, result);
    return tl.accept(this);
  }
}

/*
 *************************************************************************
 * A test case.
 *************************************************************************
 */
public class Main {
  public static void main(String[] args) {
    List test = ListF.cons(1, ListF.cons(2, ListF.cons(3, ListF.nil)));
    System.out.println(test);

    System.out.println(test.accept(new Sum()));

    System.out.println(test.accept(new Reverse()));
  }
}


/*
 *************************************************************************
 * Here is the corresponding SML code.
 * It is intended to match the Java as closely as possible.
 *************************************************************************
datatype List = Nil | Cons of int * List

fun toString (this : List) : string =
    case this of
        Nil => "nil"
      | Cons(hd, tl) => Int.toString(hd) ^ "::" ^ toString(tl)

fun sum (acceptor : List) : int =
    case acceptor of
        Nil => 0
      | Cons(hd, tl) => hd + sum(tl)

fun reverse (acceptor : List) : List =
    let fun reverseAux (acceptor : List, result : List) =
            case acceptor of
                Nil => result
              | Cons(hd, tl) => reverseAux(tl, Cons(hd,result))
    in
        reverseAux (acceptor, Nil)
    end

fun main () : unit =
    let
        val testList = Cons(1, Cons(2, Cons(3, Nil)))
        val  = print(toString(testList) ^ "\n")
        val  = print(Int.toString(sum(testList)) ^ "\n")
        val  = print(toString(copy(testList)) ^ "\n")
        val  = print(toString(reverse(testList)) ^ "\n")
    in
        ()
    end

 *************************************************************************
 */

Previous pageContentsNext page