Album Cover

Code Katas Java

Close Preview
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 an Item (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); } } }
Thanks JetBrains for hosting a webinar with this as the subject and for making the video available! The hands-on part starts around 2:15; it goes down a false path till 11:19, and then moves forward well after that.
(Reading...)
| Tweet...
Page rating: | Rate this Page