forked from emilybache/GildedRose-Refactoring-Kata
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Strategy pattern for updating item quality
- Loading branch information
Showing
6 changed files
with
87 additions
and
58 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
Java/src/main/java/com/gildedrose/AgedBrieUpdateStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
Java/src/main/java/com/gildedrose/BackstagePassesUpdateStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.gildedrose; | ||
|
||
public interface ItemUpdateStrategy { | ||
void update(Item item); | ||
} |
14 changes: 14 additions & 0 deletions
14
Java/src/main/java/com/gildedrose/StandardItemUpdateStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
Java/src/main/java/com/gildedrose/SulfurasUpdateStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.gildedrose; | ||
|
||
public class SulfurasUpdateStrategy implements ItemUpdateStrategy { | ||
@Override | ||
public void update(Item item) { | ||
// "Sulfuras, Hand of Ragnaros"는 판매일과 품질이 변하지 않음 | ||
} | ||
} |