<< Chapter < Page | Chapter >> Page > |
The public constructor:
LRStruct()
and the methods:
insertFront(...)
removeFront(...)
getFirst()
setFirst(...)
getRest()
setRest(...)
of
LRStruct
expose the structure of an
LRStruct
to the client and constitute the
intrinsic structural behavior of an
LRStruct
. They form a
minimal and complete set of methods for manipulating an
LRStruct
. Using them, a client can create an empty
LRStruct
, store data in it, and remove/retrieve data from it at will.
The method,
Object execute(IAlgo algo, Object inp)
is called the extensibility "
hook ". It allows the client to add an open-ended number of new application-dependent behaviors to the data structure
LRStruct
, such as computing its length or merging one
LRStruct
with another, without modifying any of the existing code. The application-dependent behaviors of
LRStruct
are
extrinsic behaviors and are encapsulated in a union represented by a visitor interface called
IAlgo
.
When a client programs with
LRStruct
, he/she only sees the public methods of
LRStruct
and
IAlgo
. To add a new operation on an
LRStruct
, the client writes appropriate concrete classes that implements
IAlgo
. The framework dictates the overall design of an algorithm on
LRStruct
: since an algorithm on
LRStruct
must implement
IAlgo
, there must be some concrete code for
emptyCase(...)
and some concrete code for
nonEmptyCase(...)
. For example,
public class DoSomethingWithLRS implements IAlgo {
// fields and constructor code...public Object emptyCase(LRStruct host, Object... inp) {
// some concrete code here...return some Object; // may be null.
}public Object nonEmptyCase(LRStruct host, Object... inp) {
// some concrete code here...return some Object; // may be null.
}}
As illustrated in the above, an algorithm on
LRStruct
is "
declarative " in nature. It does not involve any conditional to find out what state the
LRStruct
is in in order to perform the appropriate task. It simply "declares" what needs to be done for each state of the host
LRStruct
, and leaves it to the polymorphism machinery to make the correct call. Polymorphism is exploited to minimize flow control and reduce code complexity.
To perform an algorithm on an
LRStruct
, the client must "ask" the
LRStruct
to "execute" the algorithm and passes to it all the inputs required by the algorithm.
LRStruct myList = new LRStruct(); // an empty list
// code to call on the structural methods of myList, e.g. myList.insertFront(/*whatever*/)// Now call on myList to perform DoSomethingWithLRS:
Object result = myList.execute(new DoSomethingWithLRS(/* constructor argument list */), -2.75, "abc");
Without knowing how LRStruct is implemented, let us look an example of an algorithm on an LRStruct .
Consider the problem of inserting an Integer object
in order into a
sorted list of Integers. Let us contrast the insert in order algorithms between
IList
, the immutable list, and
LRStruct
, the mutable list.
Insert in order using factory | Insert in order using mutation |
import listFW.*;
public class InsertInOrderimplements IListAlgo {
private IListFactory _fact;public InsertInOrder(IListFactory lf) {
_fact = lf;}
/*** Simply makes a new non-empty list with
* the given parameter n as first.* @param host an empty IList.
* @param n n[0]is an Integer to be
* inserted in order into host.* @return INEList.
*/public Object emptyCase(
IEmptyList host, Object... n) {return _fact.makeNEList(n[0], host);}
/*** Based on the comparison between first
* and n, creates a new list or recur!* @param host a non-empty IList.
* @param n an Integer to be inserted in* order into host.
* @return INEList*/
public Object nonEmptyCase(INEList host, Object... n) {return (Integer)n[0]<(Integer)host.getFirst() ?_fact.makeNEList(n[0], host):_fact.makeNEList(host.getFirst(),
(IList)host.getRest().execute(this, n[0]));}
} |
import lrs.*;
public class InsertInOrderLRSimplements IAlgo {
public static final InsertInOrderLRSSingleton = new InsertInOrderLRS();
private InsertInOrderLRS() {}/**
* Simply inserts the given parameter n at* the front.
* @param host an empty LRStruct.* @param n n[0] isan Integer to be* inserted in order into host.
* @return LRStruct*/
public Object emptyCase(LRStruct host, Object... n) {
return host.insertFront(n[0]);
}/**
* Based on the comparison between first* and n, inserts at the front or recurs!
* @param host a non-empty LRStruct.* @param n n[0] is an Integer to be* inserted in order into host.
* @return LRStruct*/
public Object nonEmptyCase(LRStruct host, Object... n) {if ((Integer)n[0]<(
Integer)host.getFirst()) {return host.insertFront(n[0]);
}else {
return host.getRest().execute(this, n[0]);}
}} |
Note that the insert in order algorithm for
LRStruct need not create any new list and thus needs no factory.Download the above code:
lrs.zip |
Notification Switch
Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?