How do visitors work - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

How do visitors work

Description:

... execute(IAlgo algo, Object inp){ return ... public Object execute(IAlgo algo, Object input, LRS owner) { return algo.nonEmptyCase(owner, input); EmptyState: ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 32
Provided by: alph8
Category:
Tags: algo | visitors | work

less

Transcript and Presenter's Notes

Title: How do visitors work


1
How do visitors work?
  • This set of slides traces through the execution
    of a visitor weve seen before (the
    lengthVisitor) on a short list.
  • It shows how the runtime stack makes recursion
    possible.
  • First lets recall
  • how the visitor is defined
  • how execute is defined in the two list state
    classes

2
Length visitor
  • in the empty case the answer is zero
  • in the non-empty case the answer is one more than
    the length of the rest of the list
  • public Object emptyCase(LRS host, Object _)
  • return new Integer(0)
  • public Object nonEmptyCase(LRS host, Object _)
  • Integer lengthOfRest(Integer)(host.getRest().ex
    ecute(this,_))
  • return new Integer( 1 lengthOfRest.intValue()
    )

3
execute definition in states
  • Empty state
  • public Object execute(LRS host, IAlgo visitor,
    Object input)
  • return visitor.emptyCase(host, input)
  • NonEmpty state
  • public Object execute(LRS host, IAlgo visitor,
    Object input)
  • return visitor.nonEmptyCase(host, input)

4
Now lets trace execution of the visitor
  • LRS list new LRS()
  • list.insertFront(Wilma).insertFront(Fred)
  • IAlgo visitor new LengthVisitor()
  • Integer len (Integer) list.execute(visitor,null)
  • int length len.intValue()
  • System.out.println(The length is length)
  • Focus on the evaluation of
  • list.execute(visitor,null)

5
Tracing the execution of a visitor
list
visitor
public Object emptyCase() public Object
nonEmptyCase()
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
Fred
_datum _rest
_state
Wilma
_datum _rest
_state
6
Tracing the execution of a visitor
list
visitor
?space for return value
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
Fred
_datum _rest
_state
Wilma
_datum _rest
_state
7
Tracing the execution of a visitor
list
visitor
?space for return value
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
Fred
_datum _rest
_state
Wilma
_datum _rest
_state
8
Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
_state
this
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
Fred
_datum _rest
_state
Wilma
_datum _rest
_state
9
Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
_state
Wilma
_datum _rest
_state
10
Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
_state
Wilma
_datum _rest
_state
11
Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
_state
Wilma
_datum _rest
_state
12
Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
_state
algo
input (null)
owner
Wilma
this
_datum _rest
_state
13
Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
_state
algo
input (null)
owner
Wilma
this
_datum _rest
host
_ (null)
lengthOfRest
this
_state
14
Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
_state
algo
input (null)
owner
Wilma
this
_datum _rest
host
_ (null)
lengthOfRest
this
algo
_state
inp (null)
this
15
Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
Polymorphic method call
_state
algo
input (null)
owner
Wilma
this
_datum _rest
host
_ (null)
lengthOfRest
this
algo
_state
inp (null)
this
16
Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
_state
algo
input (null)
owner
Wilma
this
_datum _rest
host
_ (null)
lengthOfRest
this
algo
_state
inp (null)
this
algo
input (null)
owner
this
17
Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
_state
algo
input (null)
owner
Wilma
this
_datum _rest
host
_ (null)
lengthOfRest
this
algo
_state
inp (null)
this
algo
input (null)
owner
this
host
0
_ (null)
this
18
Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
_state
algo
input (null)
owner
Wilma
this
_datum _rest
host
_ (null)
lengthOfRest
this
algo
_state
inp (null)
this
algo
input (null)
owner
this
0
19
Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
_state
algo
input (null)
owner
Wilma
this
_datum _rest
host
_ (null)
lengthOfRest
this
algo
_state
inp (null)
this
0
20
Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
_state
algo
input (null)
owner
Wilma
this
_datum _rest
host
_ (null)
lengthOfRest
this
_state
0
21
Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
_state
algo
input (null)
owner
Wilma
this
_datum _rest
host
_ (null)
lengthOfRest
this
_state
1
0
22
Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
_state
algo
input (null)
owner
Wilma
this
_datum _rest
_state
1
0
23
Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
_state
Wilma
_datum _rest
_state
1
0
24
Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
_state
Wilma
_datum _rest
_state
1
0
25
Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
_state
Wilma
_datum _rest
2
_state
1
0
26
Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
_state
this
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
Fred
_datum _rest
_state
Wilma
_datum _rest
2
_state
1
0
27
Tracing the execution of a visitor
list
visitor
?space for return value
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
Fred
_datum _rest
_state
Wilma
_datum _rest
2
_state
1
0
28
Thats it!
  • Questions?

29
Exercise
  • Assume you are given an LRS containing Integer
    objects (all non-null). Write a visitor which
    traverses the list structure and calculates the
    sum of all the int values.
  • Ex If the list contains Integers for 1, 2, 3, 4
    and 5, the visitor must return an Integer
    containing 15.

30
A solution
  • public class SumVisitor implements IAlgo
  • // The answer in the case of an empty list is
    zero
  • public Object emptyCase(LRS host, Object inp)
  • // TODO Auto-generated method stub
  • return new Integer(0)
  • // The answer in the case of a non-empty list
    is the sum of the first item
  • // in the list and the sum of the rest of the
    items in the list
  • public Object nonEmptyCase(LRS host, Object inp)
  • // value stored at current position of list
  • int currentValue ((Integer)
    host.getFirst()).intValue()
  • // the sum of the rest of the values is
    calculated by
  • // recursive application of the visitor
  • int sumOfRest ((Integer) host.getRest().execut
    e(this,inp)).intValue()

31
Additional exercises
  • Write a visitor for an LRS of Integers which
    returns the largest Integer.
  • Write a visitor for an LRS of Integers, which you
    can assume appear in order from smallest to
    largest, which inserts its input in the correct
    (in order) position within the LRS.
Write a Comment
User Comments (0)
About PowerShow.com