mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 06:21:29 +00:00
README initialized
This commit is contained in:
parent
5e0d871565
commit
823afa4273
@ -0,0 +1,12 @@
|
||||
## Gilded Rose Kata - Java
|
||||
|
||||
### How to add a new updater
|
||||
Within this version of Gilded Rose kata, template method pattern is used
|
||||
|
||||
From now on it is easy to add another standard, custom or legendary items to the shop.
|
||||
How to do that;
|
||||
|
||||
* Add a new Updater class which extends either `StandardItemUpdater`, `CustomItemUpdater` or `LegendaryItemUpdater`
|
||||
* Implement required methods as your requirement.
|
||||
* Add a new registry to `registeredItemUpdaters` via `ItemUpdaterFactory.registerCustomUpdater()`
|
||||
* You are ready to call `updateQuality()` method of a `GildedRose` instance.
|
||||
@ -8,12 +8,12 @@ public class AgedBrieUpdater extends CustomItemUpdater {
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean canUpdateQuality(Item item) {
|
||||
boolean canUpdateQuality(final Item item) {
|
||||
return item.quality < HIGHEST_QUALITY;
|
||||
}
|
||||
|
||||
@Override
|
||||
int getUpdateValue(Item item) {
|
||||
int getUpdateValue(final Item item) {
|
||||
return INCREASE_NORMAL;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
package com.gildedrose;
|
||||
|
||||
public abstract class CustomItemUpdater extends ItemUpdater{
|
||||
int getNewQuality(Item item){
|
||||
int getNewQuality(final Item item){
|
||||
return Math.min(item.quality + getUpdateValue(item), HIGHEST_QUALITY);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
package com.gildedrose;
|
||||
|
||||
public abstract class ItemUpdater {
|
||||
abstract class ItemUpdater {
|
||||
static int HIGHEST_QUALITY = 50;
|
||||
static int MIN_QUALITY = 0;
|
||||
static int DEGRADE_NORMAL = -1;
|
||||
|
||||
@ -4,22 +4,22 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class ItemUpdaterFactory {
|
||||
class ItemUpdaterFactory {
|
||||
|
||||
private static final Map<String, ItemUpdater> registeredCustomUpdaters = new HashMap<>();
|
||||
private static final Map<String, ItemUpdater> registeredItemUpdaters = new HashMap<>();
|
||||
static {
|
||||
registeredCustomUpdaters.put("Aged Brie", new AgedBrieUpdater());
|
||||
registeredCustomUpdaters.put("Backstage passes to a TAFKAL80ETC concert", new BackstagePassUpdater());
|
||||
registeredCustomUpdaters.put("Sulfuras, Hand of Ragnaros", new SulfurasUpdater());
|
||||
registeredCustomUpdaters.put("Conjured Mana Cake", new ConjuredUpdater());
|
||||
registeredItemUpdaters.put("Aged Brie", new AgedBrieUpdater());
|
||||
registeredItemUpdaters.put("Backstage passes to a TAFKAL80ETC concert", new BackstagePassUpdater());
|
||||
registeredItemUpdaters.put("Sulfuras, Hand of Ragnaros", new SulfurasUpdater());
|
||||
registeredItemUpdaters.put("Conjured Mana Cake", new ConjuredUpdater());
|
||||
}
|
||||
|
||||
public static void registerCustomUpdater(String type, ItemUpdater updater ){
|
||||
registeredCustomUpdaters.put(type, updater);
|
||||
static void registerCustomUpdater(final String type, final ItemUpdater updater){
|
||||
registeredItemUpdaters.put(type, updater);
|
||||
}
|
||||
|
||||
public static ItemUpdater getItemUpdater(Item item) {
|
||||
return Optional.ofNullable(registeredCustomUpdaters.get(item.name))
|
||||
static ItemUpdater getItemUpdater(final Item item) {
|
||||
return Optional.ofNullable(registeredItemUpdaters.get(item.name))
|
||||
.orElse(new StandardItemUpdater());
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
package com.gildedrose;
|
||||
|
||||
public abstract class LegendaryItemUpdater extends ItemUpdater{
|
||||
abstract class LegendaryItemUpdater extends ItemUpdater{
|
||||
static int HIGHEST_QUALITY = 80;
|
||||
|
||||
int getNewQuality(Item item){
|
||||
int getNewQuality(final Item item){
|
||||
return Math.min(item.quality + getUpdateValue(item), HIGHEST_QUALITY);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
package com.gildedrose;
|
||||
|
||||
public class StandardItemUpdater extends ItemUpdater {
|
||||
class StandardItemUpdater extends ItemUpdater {
|
||||
|
||||
@Override
|
||||
void updateSellIn(Item item) {
|
||||
@ -8,12 +8,12 @@ public class StandardItemUpdater extends ItemUpdater {
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean canUpdateQuality(Item item) {
|
||||
boolean canUpdateQuality(final Item item) {
|
||||
return item.quality <= HIGHEST_QUALITY && item.quality > MIN_QUALITY;
|
||||
}
|
||||
|
||||
@Override
|
||||
int getUpdateValue(Item item) {
|
||||
int getUpdateValue(final Item item) {
|
||||
if (item.sellIn < 0) {
|
||||
return DEGRADE_NORMAL * 2;
|
||||
} else {
|
||||
@ -22,7 +22,7 @@ public class StandardItemUpdater extends ItemUpdater {
|
||||
}
|
||||
|
||||
@Override
|
||||
int getNewQuality(Item item) {
|
||||
int getNewQuality(final Item item) {
|
||||
return Math.min(item.quality + getUpdateValue(item), HIGHEST_QUALITY);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,19 +1,19 @@
|
||||
package com.gildedrose;
|
||||
|
||||
public class SulfurasUpdater extends LegendaryItemUpdater {
|
||||
class SulfurasUpdater extends LegendaryItemUpdater {
|
||||
@Override
|
||||
void updateSellIn(Item item) {
|
||||
System.out.print("########Never gets old ############");
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean canUpdateQuality(Item item) {
|
||||
boolean canUpdateQuality(final Item item) {
|
||||
// "Sulfuras", being a legendary item, never decreases in Quality
|
||||
return item.quality < HIGHEST_QUALITY;
|
||||
}
|
||||
|
||||
@Override
|
||||
int getUpdateValue(Item item) {
|
||||
int getUpdateValue(final Item item) {
|
||||
// "Sulfuras", being a legendary item, never decreases in Quality. Its value is always 80
|
||||
return HIGHEST_QUALITY - item.quality;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user