mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-16 06:51:27 +00:00
Introduce Strategy pattern for updating item quality
This commit is contained in:
parent
39b567780d
commit
87a76edda3
@ -0,0 +1,18 @@
|
||||
package com.gildedrose;
|
||||
|
||||
public class AgedBrieUpdateStrategy implements ItemUpdateStrategy {
|
||||
@Override
|
||||
public void update(Item item) {
|
||||
if (item.quality < 50) {
|
||||
item.quality = item.quality + 1;
|
||||
}
|
||||
|
||||
item.sellIn = item.sellIn - 1;
|
||||
|
||||
if (item.sellIn < 0) {
|
||||
if (item.quality < 50) {
|
||||
item.quality = item.quality + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
package com.gildedrose;
|
||||
|
||||
public class BackstagePassesUpdateStrategy implements ItemUpdateStrategy {
|
||||
@Override
|
||||
public void update(Item item) {
|
||||
if (item.quality < 50) {
|
||||
item.quality = item.quality + 1;
|
||||
|
||||
if (item.sellIn < 11) {
|
||||
if (item.quality < 50) {
|
||||
item.quality = item.quality + 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (item.sellIn < 6) {
|
||||
if (item.quality < 50) {
|
||||
item.quality = item.quality + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
item.sellIn = item.sellIn - 1;
|
||||
|
||||
if (item.sellIn < 0) {
|
||||
item.quality = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,72 +1,28 @@
|
||||
package com.gildedrose;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
class GildedRose {
|
||||
public static final String DEFAULT_STRATEGY = "default";
|
||||
Item[] items;
|
||||
HashMap<String, ItemUpdateStrategy> strategies;
|
||||
|
||||
public GildedRose(Item[] items) {
|
||||
this.items = items;
|
||||
this.strategies = new HashMap<>();
|
||||
this.strategies.put("Aged Brie", new AgedBrieUpdateStrategy());
|
||||
this.strategies.put("Backstage passes to a TAFKAL80ETC concert", new BackstagePassesUpdateStrategy());
|
||||
this.strategies.put("Sulfuras, Hand of Ragnaros", new SulfurasUpdateStrategy());
|
||||
this.strategies.put(DEFAULT_STRATEGY, new StandardItemUpdateStrategy());
|
||||
}
|
||||
|
||||
public void updateQuality() {
|
||||
for (Item item : items) {
|
||||
// "Aged Brie"와 "Backstage passes"가 아닌 경우
|
||||
// 품질이 0보다 크면 "Sulfuras, Hand of Ragnaros" 제외 아이템 품질을 1 감소
|
||||
if (!item.name.equals("Aged Brie")
|
||||
&& !item.name.equals("Backstage passes to a TAFKAL80ETC concert")) {
|
||||
if (item.quality > 0) {
|
||||
if (!item.name.equals("Sulfuras, Hand of Ragnaros")) {
|
||||
item.quality = item.quality - 1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// "Aged Brie" 또는 "Backstage passes"인 경우 품질 50 미만이면 품질 1 증가
|
||||
// "Backstage passes"의 경우 품질이 50 미만이고 판매일이 11일 미만인 경우 추가로 1 증가
|
||||
// 판매일이 6일 미만인 경우 또한 추가로 1 증가
|
||||
if (item.quality < 50) {
|
||||
item.quality = item.quality + 1;
|
||||
|
||||
if (item.name.equals("Backstage passes to a TAFKAL80ETC concert")) {
|
||||
if (item.sellIn < 11) {
|
||||
if (item.quality < 50) {
|
||||
item.quality = item.quality + 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (item.sellIn < 6) {
|
||||
if (item.quality < 50) {
|
||||
item.quality = item.quality + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// "Sulfuras, Hand of Ragnaros"가 아닌 경우 판매일을 1 줄임
|
||||
if (!item.name.equals("Sulfuras, Hand of Ragnaros")) {
|
||||
item.sellIn = item.sellIn - 1;
|
||||
}
|
||||
|
||||
// 판매일이 지난 경우
|
||||
// "Aged Brie"가 아니고, "Backstage passes"가 아닌 경우 품질이 0 보다 크면 줄임
|
||||
// "Backstage passes"가 아닌 경우 품질을 0으로 변경
|
||||
// "Aged Brie"인 경우 품질이 50 미만이면 증가
|
||||
if (item.sellIn < 0) {
|
||||
if (!item.name.equals("Aged Brie")) {
|
||||
if (!item.name.equals("Backstage passes to a TAFKAL80ETC concert")) {
|
||||
if (item.quality > 0) {
|
||||
if (!item.name.equals("Sulfuras, Hand of Ragnaros")) {
|
||||
item.quality = item.quality - 1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
item.quality = item.quality - item.quality;
|
||||
}
|
||||
} else {
|
||||
if (item.quality < 50) {
|
||||
item.quality = item.quality + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
getItemUpdateStrategy(item).update(item);
|
||||
}
|
||||
}
|
||||
|
||||
private ItemUpdateStrategy getItemUpdateStrategy(Item item) {
|
||||
return strategies.getOrDefault(item.name, strategies.get(DEFAULT_STRATEGY));
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,5 @@
|
||||
package com.gildedrose;
|
||||
|
||||
public interface ItemUpdateStrategy {
|
||||
void update(Item item);
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
package com.gildedrose;
|
||||
|
||||
public class StandardItemUpdateStrategy implements ItemUpdateStrategy {
|
||||
@Override
|
||||
public void update(Item item) {
|
||||
item.sellIn -= 1;
|
||||
if (item.quality > 0) {
|
||||
item.quality = item.quality - 1;
|
||||
}
|
||||
if (item.sellIn < 0 && item.quality > 0) {
|
||||
item.quality = item.quality - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,8 @@
|
||||
package com.gildedrose;
|
||||
|
||||
public class SulfurasUpdateStrategy implements ItemUpdateStrategy {
|
||||
@Override
|
||||
public void update(Item item) {
|
||||
// "Sulfuras, Hand of Ragnaros"는 판매일과 품질이 변하지 않음
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user