we don't want to create additional class, just a better dsesign
abstract class Bag {
protected Vector bag;
public Bag() {
bag = new Vector();
}
public void add (Object o) {
bag.add (o);
}
}
class ToyBag extends Bag {
public Toy remove() {
return (Toy) bag.remove(0);
}
}
class TrashBag extends Bag {
// This is strange. But, it preserves the original class
// structure, so interacting objects will never know about the
// refactoring. It also conveys the minimalist nature of trash
// bags :-)
}
class LaundryBag extends Bag {
public Clothing get(Clothing item) {
for (int index=0; index < bag.size(); index++) {
if (bag.elementAt(index).equals(item)) {
return (Clothing) bag.remove(index);
}
}
return null;
}
}