mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 14:31:28 +00:00
different implementation of constructor
This commit is contained in:
parent
7b0e2daa3d
commit
9b308c8e22
@ -7,15 +7,17 @@ import com.gildedrose.item.QualityValues;
|
|||||||
|
|
||||||
class GildedRose {
|
class GildedRose {
|
||||||
|
|
||||||
|
private final CustomisedItemFactory itemFactory;
|
||||||
Item[] items;
|
Item[] items;
|
||||||
|
|
||||||
public GildedRose(Item[] items) {
|
public GildedRose(Item[] items) {
|
||||||
this.items = items;
|
this.items = items;
|
||||||
|
this.itemFactory = new CustomisedItemFactory();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateQuality() {
|
public void updateQuality() {
|
||||||
for (Item item : items) {
|
for (Item item : items) {
|
||||||
customisedItem(item).updateState();
|
itemFactory.customiseItem(item).updateState();
|
||||||
if (hasReachedLowestQualityValue(item)) {
|
if (hasReachedLowestQualityValue(item)) {
|
||||||
item.quality = QualityValues.lowestValuePossible();
|
item.quality = QualityValues.lowestValuePossible();
|
||||||
} else if (hasReachedHighestQualityValue(item)) {
|
} else if (hasReachedHighestQualityValue(item)) {
|
||||||
@ -24,10 +26,6 @@ class GildedRose {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private CustomisedItem customisedItem(Item item) {
|
|
||||||
return new CustomisedItemFactory(item).customiseItem(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean hasReachedLowestQualityValue(Item item) {
|
private boolean hasReachedLowestQualityValue(Item item) {
|
||||||
return item.quality < QualityValues.lowestValuePossible();
|
return item.quality < QualityValues.lowestValuePossible();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,30 +2,32 @@ package com.gildedrose.item;
|
|||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
public class CustomisedItemFactory {
|
public class CustomisedItemFactory {
|
||||||
|
|
||||||
private final static Map<String, CustomisedItem> ITEM_TYPES_LIST = new HashMap<>();
|
private final static Map<String, Function<Item, CustomisedItem>> ITEM_TYPES_LIST = new HashMap<>();
|
||||||
public final static String SULFURAS = "Sulfuras, Hand of Ragnaros";
|
public final static String SULFURAS = "Sulfuras, Hand of Ragnaros";
|
||||||
public final static String BRIE = "Aged Brie";
|
public final static String BRIE = "Aged Brie";
|
||||||
public final static String BACKSTAGE_PASSES_ITEM = "Backstage passes to a TAFKAL80ETC concert";
|
public final static String BACKSTAGE_PASSES_ITEM = "Backstage passes to a TAFKAL80ETC concert";
|
||||||
public final static String CONJURED_ITEM = "Conjured";
|
public final static String CONJURED_ITEM = "Conjured";
|
||||||
|
|
||||||
public CustomisedItemFactory(Item item) {
|
public CustomisedItemFactory() {
|
||||||
ITEM_TYPES_LIST.put(SULFURAS, new Sulfuras());
|
ITEM_TYPES_LIST.put(SULFURAS, (item) -> new Sulfuras());
|
||||||
ITEM_TYPES_LIST.put(BRIE, new AgedBrie(item));
|
ITEM_TYPES_LIST.put(BRIE, AgedBrie::new);
|
||||||
ITEM_TYPES_LIST.put(BACKSTAGE_PASSES_ITEM, new BackstagePassesItem(item));
|
ITEM_TYPES_LIST.put(BACKSTAGE_PASSES_ITEM, BackstagePassesItem::new);
|
||||||
ITEM_TYPES_LIST.put(CONJURED_ITEM, new ConjuredItem(item));
|
ITEM_TYPES_LIST.put(CONJURED_ITEM, ConjuredItem::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
public CustomisedItem customiseItem(Item item) {
|
public CustomisedItem customiseItem(Item item) {
|
||||||
if (isStandardItem(item)) {
|
String name = item.name;
|
||||||
|
if (isStandardItem(name)) {
|
||||||
return new StandardItem(item);
|
return new StandardItem(item);
|
||||||
}
|
}
|
||||||
return ITEM_TYPES_LIST.get(item.name);
|
return ITEM_TYPES_LIST.get(name).apply(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isStandardItem(Item item) {
|
private boolean isStandardItem(String name) {
|
||||||
return !ITEM_TYPES_LIST.keySet().contains(item.name);
|
return !ITEM_TYPES_LIST.keySet().contains(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user