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

Compare with Current View Page History

« Previous Version 10 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.

 In viewing the UML diagrams below, keep in mind that a class or interface always inherits all of the methods of its parents, i.e. everything above it.   Just because a class or interface only shows 2 methods in its little box in the UML diagram doesn't mean that's all the methods it has--look at all the methods in its superclasses/superinterfaces too!

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. 

All Garments have an "adjective" field that is used to differentiate different instances of Shirts and Pants and Socks, e.g. a "blue" Shirt vs. a "red" Shirt.

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!"


The SameTypeGarmentVisitor is a utility visitor that can be used to return a true or false if two Garment objects are the same type, i.e. a Shirt and a Shirt or a Pants and a Pants, regardless of their "adjectives". 


Lists package

BiLists are used to represent the various piles of clothes.





sadfsdf

sadfsdaf




 

  • No labels