/** * Represents an abstract Person, independent of gender. * @dependency ITask accepts */ abstract class APerson { /** * Executes the appropriate case of the ITask visitor, returning the result. * @param task The visitor to execute. * @param param An arbitrary parameter that might be used by the visitor. * @return The return result of the appropriate case of the visitor. */ public abstract Object execute(ITask task, Object param); } /** * Represents a female person. */ class Female extends APerson { /** * Executes the femaleCase of the ITask visitor, handing itself as the host. * @param task The visitor to execute * @param param An arbitrary parameter passed unchanged to the femaleCase of the visitor. * @return The return value of the female case of the visitor. */ public Object execute(ITask task, Object param) { return task.femaleCase(this, param); } } /** * Represents a male person. */ class Male extends APerson { /** * Executes the maleCase of the ITask visitor, handing itself as the host. * @param task The visitor to execute * @param param An arbitrary parameter passed unchanged to the maleCase of the visitor. * @return The return value of the male case of the visitor. */ public Object execute(ITask task, Object param) { return task.maleCase(this, param); } } /** * Represents an abstract task for an abstract person to perform. * @dependency APerson uses */ interface ITask { /** * The task for a female person. * @param femaleHost The female person who is executing the task. * @param param An arbitrary parameter for the task. * @return The return value of the task. */ public Object femaleCase(Female femaleHost, Object param); /** * The task for a male person. * @param maleHost The male person who is executing the task. * @param param An arbitrary parameter for the task. * @return The return value of the task. */ public Object maleCase(Male maleHost, Object param); } /** * The task of eating. * */ class EatTask implements ITask { /** * The eating task for a female person. * @param femaleHost The female person executing this task. * @param food Not used * @return Always returns "No thanks, may I have a salad with dressing on the side, please?" */ public Object femaleCase(Female femaleHost, Object food) { return "No thanks, may I have a salad with dressing on the side, please?"; } /** * The eating task for a male person. * @param femaleHost The male person executing this task. * @param food The food being eaten as a String. * @return Always returns "You got a beer to go with that "+[food input value] */ public Object maleCase(Male maleHost, Object food) { return "You got a beer to go with that " + food + "?"; } } /** * The task of person getting dressed. */ class GetDressedTask implements ITask { /** * The getting dressed task for a female person. * @param femaleHost The female person executing this task. * @param param not used * @return Always returns "I'll be ready in a minute!" */ public Object femaleCase(Female femaleHost, Object param) { return "I'll be ready in a minute!"; } /** * The getting dressed task for a male person. * @param femaleHost The male person executing this task. * @param param not used.. * @return Always returns "Does "black tie" mean I can't wear shorts?" */ public Object maleCase(Male maleHost, Object param) { return "Does \"black tie\" mean I can't wear shorts?"; } } /** * The weekend tasks of a person. */ class WeekendTask implements ITask { /** * The weekend tasks for a female person. * @param femaleHost The female person executing this task. * @param time The number of hours to complete the task as a String * @return Always returns "Only have "+[time value]+" hours, so let's clean the house, wash the clothes and weed the garden!" */ public Object femaleCase(Female femaleHost, Object time) { return "Only have "+time+" hours, so let's clean the house, wash the clothes and weed the garden!"; } /** * The weekend tasks for a male person. * @param maleHost The male person executing this task. * @param time Not used. * @return Always returns "Where's the remote?" */ public Object maleCase(Male maleHost, Object time) { return "Where's the remote?"; } } /** * This class holds a Male and Female person instance and randomly assigns them to process the incoming ITask. * This class is used to demonstrate that the visitor (ITask) doesn't care what host (APerson) is executing it--the visitor will always return the correct result for that host. * */ class Tasker { APerson f = new Female(); APerson m = new Male(); /** * Runs the given task on a randomly chosen person. The type of the selected host is printed to System.out. * @param t The task to perform. * @param p Passed on to the visitor unaltered. * @return The return value of the visitor. */ Object doTask(ITask t, Object p) { if(Math.random() > 0.5) { System.out.println("Female host selected:"); return f.execute(t, p); } else { System.out.println("Male host selected:"); return m.execute(t, p); } } }