<< Chapter < Page | Chapter >> Page > |
InsertInOrderWithFactory.java |
import listFW.*;
/*** Inserts an Integer into an ordered host list, assuming the host list contains
* only Integer objects. Has no knowledge of how IList is implemented. Must* make use of a list factory (IListFactory) to create IList objects instead of
* calling the constructors of concrete subclasses directly.*/
public class InsertInOrderWithFactory implements IListAlgo {private IListFactory _listFact;
public InsertInOrderWithFactory(IListFactory lf) {_listFact = lf;
}/**
* Simply makes a new non-empty list with the given inp parameter as first.* @param host an empty IList.
* @param inp inp[0]is an Integer to be inserted in order into host.
*/public Object emptyCase(IMTList host, Object... inp) {
return _listFact.makeNEList(inp[0], host);
}/**
* Recur!* @param host a non-empty IList.
* @param inp inp[0]is an Integer to be inserted in order into host.
*/public Object nonEmptyCase(INEList host, Object... inp) {
int n = (Integer)inp[0];
int f = (Integer)host.getFirst();return n<f ?
_listFact.makeNEList(inp[0], host):
_listFact.makeNEList(host.getFirst(),(IList)host.getRest().execute(this, inp[0]));}
} |
The above algorithm only "talks" to the list structure it operates on at the highest level of abstraction specified by
IList
and
IListFactory
. It does know and does not care how
IList
and
IListFactory
are implemented. Yet it can be proved to be correct. This algorithm can be plugged into any system that subscribes to the abstract specification prescribed by
IList
,
IListAlgo
, and
IListFactory
.
IList
as
private static classes and thus hide them from all external code.
CompositeListFactory.java |
package listFW.factory;
import listFW.*;/**
* Manufactures concrete IMTList and INEList objects. Has only one* instance referenced by CompositeListFactory.Singleton.
* MTList and NEList are static nested classes and hidden from all external* client code. The implementations for MTList and NEList are the same as
* before but completely invisible to the outside of this factory.*/
public class CompositeListFactory implements IListFactory {/**
* Note the use of private static.*/
private static class MTList implements IMTList {public final static MTList Singleton = new MTList ();
private MTList() {}
final public Object execute(IListAlgo algo, Object... inp) {return algo.emptyCase(this, inp);
}public String toString() {
return "()";}
} |
/**
* Note the use of private static.*/private static class NEList implements INEList {
private Object _first;private IList _rest;
public NEList(Object dat, IList rest) {_first = dat;
_rest = rest;}
final public Object getFirst() {return _first;
}final public IList getRest() {
return _rest;}
final public Object execute(IListAlgo algo, Object... inp) {return algo.nonEmptyCase(this, inp);
}public String toString() {
return (String)ToStringAlgo.Singleton.nonEmptyCase(this);}
} |
/**
* Singleton Pattern*/
public static final CompositeListFactory Singleton = new CompositeListFactory();private CompositeListFactory() {
}/**
* Creates an empty list.* @return an IMTList object.
*/public IMTList makeEmptyList() {
return MTList.Singleton;}
/*** Creates a non-empty list containing a given first and a given rest.
* @param first a data object.* @param rest != null, the rest of the non-empty list to be manufactured.
* @return an INEList object containing first and rest*/
public INEList makeNEList(Object first, IList rest) {return new NEList(first, rest);
}} |
Notification Switch
Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?