You are here

Examples

15 January, 2016 - 09:02
Available under Creative Commons-ShareAlike 4.0 International License. Download for free at http://cnx.org/contents/402b20ad-c01f-45f1-9743-05eadb1f710e@37.6

Example: Reversing a list using factory and anonymous inner class helper

Write Reverse such that it takes one parameter, the IListFactory, but such that its helper only takes one parameter (other than the host list) which is the accumulated list.

public class Reverse implements IListAlgo { public static final Reverse Singleton = new Reverse(); private Reverse() {} public Object emptyCase(IMTList host0, Object... fac) { return ((IListFactory)fac[0]).makeEmptyList(); } public Object nonEmptyCase(INEList host0, Object... fac) { final IListFactory f = (IListFactory) fac[0]; 11 final so that the anon. inner class can access it. return host0.getRest().execute(new IListAlgo() { public Object emptyCase(IMTList host1, Object... acc) { return acc[0]; } public Object nonEmptyCase(INEList host1, Object... acc) { return host1.getRest().execute(this, f.makeNEList(host1.getFirst(), (IList) acc[0])); } },f.makeNEList(host0.getFirst(), f.makeEmptyList())); } }

}

Example: Ant World

Imagine a world of ants that live in a one-dimensional space. A queen ant can make a bunch of worker ants. Each time she gives birth to a worker ant, she gives it a name. A worker ant can always tell what its name is. A worker ant from a particular colony can always calculate its distance from its queen. A worker ant can also move its queen to a different location. Wherever the queen moves to, ALL of her workers always know their relative distance from her. We want to keep track of all the ants in our ant world and all the ants in each of the existing colonies. We want to model the fact that each queen produces its own worker ants, each one which can move its queen around without telling the other ants in the same colony, yet ALL of the ants in the same colony would know where their queen is.

The above can be modeled by a Queen class with an abstract Worker inner class as shown below. This example illustrates the differences between static and non-static fields, methods and embedded classes.

Queen.java

/** *  Models ants living in a 1-dimensional world * * *  The Worker inner objects have direct access to the location of its outer *  Queen object. * *  @author A.L. Cox *  @author D.X. Nguyen *  @author S.B. Wong * @since 02/07/2003 */ public class Queen { /** *  The total number of ants (queens and workers for all the queens) that * currently exist in our ant world. * *  Why is this field static? */ private static int _ants; /** *  The location of this Queen object with respect to 0. * *  Why is this field non-static? */ private int _origin; /** *  The total numbers of living worker ants produced by this Queen object. * Why is this field non-static? */ private int workers; /** * Is part of a Queen instance, just like the origin field and the * makeWorker() method are parts of a Queen instance. * Any  concrete implementation of Worker must implement the getName() * method to return the name given by its Queen at birth time. * * Why can't this class be static? */ public abstract class Worker { /** * The location of this Worker object. */ private int _location; /** * Increments ants and workers because every time a Worker is * instantiated, a new ant is added to our ant world, a new worker ant * is added to the colony of this outer Queen object. 

Exercise

Write a JUnit test program for the Queen and Worker classes.