Gilded Rose
One Solution
This is definite progress, but there's more I'd consider doing to this solution:
- Put better tests around the new feature - the test cases don't justify the code for dealing with expired Conjured items.
- Get rid of the
categorize()
method - make the category part of anItem
(or wrapper if we really can't change that class) when the item is created. - Consider whether the increment and decrement methods can fold together (and maybe add an argument for how much to change by). (This method really belongs on
Item
.) - Split out the category classes to separate files. (I'd also consider a
RegularItemCategory
class rather than using the parent for that.)
class GildedRose {
Item[] items; public GildedRose(Item[] items) {
this.items = items;
} public void updateQuality() {
for (Item item : items) {
ItemCategory category = categorize(item);
category.updateOneItem(item);
}
} private ItemCategory categorize(Item item) {
if (item.name.equals("Sulfuras, Hand of Ragnaros")) {
return new Legendary();
}
if (item.name.equals("Aged Brie")) {
return new Cheese();
}
if (item.name.equals("Backstage passes to a TAFKAL80ETC concert")) {
return new BackstagePass();
}
if (item.name.startsWith("Conjured"))
return new Conjured();
return new ItemCategory();
} private class ItemCategory {
protected void incrementQuality(Item item) {
if (item.quality < 50) {
item.quality = item.quality + 1;
}
} protected void decrementQuality(Item item) {
if (item.quality > 0) {
item.quality = item.quality - 1;
}
} protected void updateExpired(Item item) {
decrementQuality(item);
} protected void updateSellIn(Item item) {
item.sellIn = item.sellIn - 1;
} protected void updateQuality(Item item) {
decrementQuality(item);
} private void updateOneItem(Item item) {
updateQuality(item); updateSellIn(item); if (item.sellIn < 0) {
updateExpired(item);
}
}
} private class Legendary extends ItemCategory {
protected void updateExpired(Item item) {} protected void updateSellIn(Item item) {} protected void updateQuality(Item item) {}
} private class Cheese extends ItemCategory {
protected void updateExpired(Item item) {
incrementQuality(item);
} protected void updateQuality(Item item) {
incrementQuality(item);
}
} private class BackstagePass extends ItemCategory {
protected void updateExpired(Item item) {
item.quality = 0;
} protected void updateQuality(Item item) {
incrementQuality(item); if (item.sellIn <= 10) {
incrementQuality(item);
} if (item.sellIn <= 5) {
incrementQuality(item);
}
}
} private class Conjured extends ItemCategory {
protected void updateExpired(Item item) {
decrementQuality(item);
decrementQuality(item);
} protected void updateQuality(Item item) {
decrementQuality(item);
decrementQuality(item);
}
}
}
(Reading...)
Formatting Hints:
To format... | use this markup: |
---|---|
source code | {source:lang=java|lineNumbers=false}...{source} (Supported languages: java, cpp, csharp, c, python.) |
bold text | __some text__ |
italicized text | ~~some text~~ |
underlined text | @@some text@@ |
a method or class name | ``myMethod(...)`` |