package schemeFactory; /** * Concrete IListFactory to manufacture AList as composites. * EmptyList and NEList are static nested classes and hidden from all external * client code. The implementations for EmptyList and NEList are the same as * before but completely invisible to the outside of this factory. * @author D.X. Nguyen */ public class CompositeListFactory implements IListFactory { /** * Note the use of private static. */ private static class EmptyList extends AList { public final static EmptyList Singleton = new EmptyList(); private EmptyList() { } public Object getFirst() { throw new IllegalArgumentException("Empty list has no first!"); } public AList getRest() { throw new IllegalArgumentException("Empty list has no first!"); } public Object execute(IListAlgo algo, Object inp) { return algo.emptyCase(this, inp); } } /** * Note the use of private static. */ private static class NEList extends AList { private Object _first; private AList _rest; public NEList(Object dat, AList tail) { _first = dat; _rest = tail; } public Object getFirst() { return _first; } public AList getRest() { return _rest; } public Object execute(IListAlgo algo, Object inp) { return algo.nonEmptyCase(this, inp); } } public AList makeEmptyList() { return EmptyList.Singleton; } public AList makeNEList(Object first, AList tail) { return new NEList(first, tail); } }