You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Next »

HW10 Code Details

 Most of the supplied code is support code that you do not have to worry about.   You are certainly free to use whatever you wish, but only the classes listed here should be needed.    

You WILL need to make anonymous inner classes and possible some named classes if you feel you need them.   The staff's solution only uses anonymous inner classes, so it definitely can be done that way.

Student Package

A student is wearing a shirt, pants and socks.   The student also has references to several lists (ordered piles) of garments:  clean shirts, clean pants, clean socks, a dirty clothes pile and a laundry room which is a list of list of garments because it represents the collection of multiple loads of laundered garments that have been cleaned, but not yet folded and returned to the individual clean garment piles.


Garment Package

 Garments are of 4 types:

  1. Shirts - represents shirts of various types
  2. Pants - represents pants of various types
  3. Socks - represents socks of various types
  4. NullGarment - represents the absence of a garment, such as one might get on a failed search for a garment. 

Garments support a visitor, GarmentVisitor, which has cases for each type of visitor.   Never test for the type of a Garment object!   Always delegate to it by having the garment object accept a visitor whose different cases provide the garment-type-dependent processing you desire.

AGarmentVisitor is a convenience class that provides concrete default behavior for all the cases.    If you only need to specify one or two cases and just return a default value for the rest, create your visitor by sub-classing AGarmentVisitor and overriding only the cases you desire.   Note that the AGarmentVisitor's constructor requires an input parameter, the default value to use for the non-overridden cases.

 Here's an example of using AGarmentVisitor to write a little visitor to return either "Not a Sock!" or "Got some socks!" depending on whether the visitor is accepted by a Socks object or some other kind of Garment object.

GarmentVisitor isSockGarmentVisitor = new AGarmentVisitor<String>("Not a Sock!") {
   public String forSocks(Socks sockHost) {
      return "Got some socks!";
   }
}

aShirt.accept(isSockGarmentVisitor) --> "Not a Sock!"
aPants.accept(isSockGarmentVisitor) --> "Not a Sock!"
aNullGarment.accept(isSockGarmentVisitor) --> "Not a Sock!"
aSock.accept(isSockGarmentVisitor) --> "Got some socks!"



!uml_list.png|align=center! 

sadfsdf

sadfsdaf




 

  • No labels