|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface IListAlgo
Represents an abstract algorithm to be executed by a host IList. Plays the role of a "visitor" in the design pattern language.
In non OO programming paradigms, an algorithm on lists is expressed as a function or procedure that takes in a list as parameter. In particular, our Comp 210 Scheme class prescribes a 'template' for writing functions that operates on lists as follows.
(define f (lst) (cond [(empty? lst) ...] [(cons? lst) ...(first lst) ...(f (rest lst)...]))In our OO formulation, an alogrithm on lists is an interface that has two methods, each takes in a list as a parameter:
One need not (and should not) check for the type of a list before applying an algorithm to it. THERE IS NO cond!
Method Summary | |
---|---|
Object |
emptyCase(IEmptyList host,
Object... inp)
Operates on IEmptyList , the empty list. |
Object |
nonEmptyCase(INEList host,
Object... inp)
Operates on INEList , a non-empty list. |
Method Detail |
---|
Object emptyCase(IEmptyList host, Object... inp)
IEmptyList
, the empty list. Since
IEmptyList
has only one method, execute()
,
there is not much the host list can do here. So the code for the empty
case usually involves:inp
, and perhaps
host.execute(..., ...)
.[(empty? lst) ...]
part of the
(Comp 210) Scheme function template.
host
- the IEmptyList
that is executing this algorithm.inp
- a variable number of input parameters that can be used
for any purpose.
Object nonEmptyCase(INEList host, Object... inp)
INEList
, a non-empty list. The host list now has
a first and a rest. So the code here usually involves what
host.getFirst()
and host.getRest()
can do to
accomplish the task at hand.host.getFirst()
is simply a data Object
that the host list holds. It is problem-specific, and thus what it can
do depends on the problem the current algorithm is trying to solve.
host.getRest()
in contrast is an IList
!
What can an IList
do? execute
an alogrithm
IListAlgo
with some input. What IListAlgo
can
that be? The current algorithm that is being executed is as good a
candidate as any other IListAlgo
. In Java, we use the key
word this
to reference the current receiver of the
method call. Having the rest of host (recursively) execute the current
algorithm is expressed in Java as:
host.getRest().execute(this, ...)
.To summarize, the code for the non-empty case usually involves:
host.getFirst()
, and the recursive call
host.getRest().execute(this, something involving inp)
.[(cons? lst) ...(first lst) ...(f (rest lst))...]
part of the (Comp 210) Scheme function template.
host
- the INEList
that is executing this algorithminp
- variable number input parameters that can be used
for any purpose.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |