mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 14:31:28 +00:00
Implement Factory Design Pattern
This commit is contained in:
parent
1905a68b56
commit
32b45befe9
@ -3,13 +3,13 @@ package com.gildedrose;
|
||||
/**
|
||||
* Class for Aged Brie item inherited from NormalItem
|
||||
*/
|
||||
public class AgedBrie extends NormalItem {
|
||||
public class AgedBrie extends BaseItem implements ItemInterface {
|
||||
|
||||
public AgedBrie(Item item) {
|
||||
this.item=item;
|
||||
}
|
||||
|
||||
public void updateQuaility() {
|
||||
public void updateQuality() {
|
||||
if (itemHasExpired()) {
|
||||
increaseQualityBy(2);
|
||||
} else {
|
||||
|
||||
@ -6,12 +6,12 @@ package com.gildedrose;
|
||||
* Business rules are inherited from NormalItem with more conditions:
|
||||
* Quality increases by 2 when there are 10 days or less and by 3 when there are 5 days or less
|
||||
*/
|
||||
public class BackStageItem extends NormalItem {
|
||||
public class BackStageItem extends BaseItem implements ItemInterface {
|
||||
public BackStageItem(Item item) {
|
||||
this.item=item;
|
||||
}
|
||||
|
||||
public void update() {
|
||||
public void updateQuality() {
|
||||
increaseQualityBy(1);
|
||||
if (item.sellIn < 10) {
|
||||
increaseQualityBy(1);
|
||||
|
||||
47
Java/src/main/java/com/gildedrose/BaseItem.java
Normal file
47
Java/src/main/java/com/gildedrose/BaseItem.java
Normal file
@ -0,0 +1,47 @@
|
||||
package com.gildedrose;
|
||||
|
||||
import com.sun.xml.internal.rngom.parse.host.Base;
|
||||
|
||||
public abstract class BaseItem implements ItemInterface {
|
||||
public Item item;
|
||||
public static final int MAX_QUAILITY_FOR_AN_ITEM = 50;
|
||||
|
||||
public BaseItem() {}
|
||||
|
||||
public BaseItem(Item item) {
|
||||
this.item=item;
|
||||
}
|
||||
|
||||
protected boolean itemHasExpired() {
|
||||
boolean condition;
|
||||
if (item.sellIn < 0) {
|
||||
condition = true;
|
||||
} else {
|
||||
condition = false;
|
||||
}
|
||||
return condition;
|
||||
}
|
||||
|
||||
protected void increaseQualityBy(int factor) {
|
||||
item.quality += factor;
|
||||
qualityOfAnItemIsNotMoreThan(MAX_QUAILITY_FOR_AN_ITEM);
|
||||
}
|
||||
|
||||
protected void decreaseQualityBy(int factor) {
|
||||
item.quality -= factor;
|
||||
qualityOfAnItemIsNeverNegative();
|
||||
}
|
||||
|
||||
private void qualityOfAnItemIsNotMoreThan(int limit) {
|
||||
if (item.quality > limit) {
|
||||
item.quality = limit;
|
||||
}
|
||||
}
|
||||
|
||||
private void qualityOfAnItemIsNeverNegative() {
|
||||
if (item.quality < 0) {
|
||||
item.quality = 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,9 +1,11 @@
|
||||
package com.gildedrose;
|
||||
|
||||
import com.sun.xml.internal.rngom.parse.host.Base;
|
||||
|
||||
/**
|
||||
* Class for the item ConjuredItem inherited from NormalItem
|
||||
*/
|
||||
public class ConjuredItem extends NormalItem {
|
||||
public class ConjuredItem extends BaseItem implements ItemInterface {
|
||||
public ConjuredItem(Item item) {
|
||||
this.item=item;
|
||||
}
|
||||
|
||||
@ -1,45 +1,33 @@
|
||||
package com.gildedrose;
|
||||
|
||||
import com.sun.xml.internal.rngom.parse.host.Base;
|
||||
|
||||
/**
|
||||
* Class which update quality for all items
|
||||
*/
|
||||
class GildedRoseItem {
|
||||
class GildedRoseItem {
|
||||
Item[] items;
|
||||
public static final String SULFURA = "Sulfuras, Hand of Ragnaros";
|
||||
public static final String AGED_BRIE = "Aged Brie";
|
||||
public static final String BACKSTAGE = "Backstage passes to a TAFKAL80ETC concert";
|
||||
public static final String CONJURED = "Conjured Mana Cake";
|
||||
|
||||
private ItemFactory itemFactory;
|
||||
|
||||
public GildedRoseItem(Item[] items) {
|
||||
this.items = items;
|
||||
itemFactory = new ItemFactory();
|
||||
}
|
||||
|
||||
private void updateNumberOfdayToSellRemaining(Item item) {
|
||||
item.sellIn -= 1;
|
||||
}
|
||||
|
||||
private void updateQualityItem(Item item) {
|
||||
if (item.name.equals(AGED_BRIE)) {
|
||||
AgedBrie agedBrieItem = new AgedBrie(item);
|
||||
agedBrieItem.updateQuaility();
|
||||
} else if (item.name.equals(BACKSTAGE)) {
|
||||
BackStageItem backStageItem = new BackStageItem(item);
|
||||
backStageItem.update();
|
||||
} else if (item.name.equals(CONJURED)) {
|
||||
ConjuredItem conjuredItem = new ConjuredItem(item);
|
||||
conjuredItem.updateQuality();
|
||||
} else {
|
||||
NormalItem normalItem = new NormalItem(item);
|
||||
normalItem.updateQuality();
|
||||
}
|
||||
}
|
||||
|
||||
public void updateQuality() {
|
||||
for (Item item : items) {
|
||||
if (item.name.equals(SULFURA)) {continue;}
|
||||
if (item.name.equals(SULFURA)) {
|
||||
continue;
|
||||
}
|
||||
ItemInterface typeItem = itemFactory.createItemType(item);
|
||||
updateNumberOfdayToSellRemaining(item);
|
||||
updateQualityItem(item);
|
||||
typeItem.updateQuality();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
23
Java/src/main/java/com/gildedrose/ItemFactory.java
Normal file
23
Java/src/main/java/com/gildedrose/ItemFactory.java
Normal file
@ -0,0 +1,23 @@
|
||||
package com.gildedrose;
|
||||
|
||||
public class ItemFactory {
|
||||
|
||||
|
||||
public static final String AGED_BRIE = "Aged Brie";
|
||||
public static final String BACKSTAGE = "Backstage passes to a TAFKAL80ETC concert";
|
||||
public static final String CONJURED = "Conjured Mana Cake";
|
||||
|
||||
|
||||
public ItemInterface createItemType(Item item) {
|
||||
if (item.name.equals(AGED_BRIE)) {
|
||||
return new AgedBrie(item);
|
||||
} else if (item.name.equals(BACKSTAGE)) {
|
||||
return new BackStageItem(item);
|
||||
} else if (item.name.equals(CONJURED)) {
|
||||
return new ConjuredItem(item);
|
||||
} else {
|
||||
return new NormalItem(item);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
5
Java/src/main/java/com/gildedrose/ItemInterface.java
Normal file
5
Java/src/main/java/com/gildedrose/ItemInterface.java
Normal file
@ -0,0 +1,5 @@
|
||||
package com.gildedrose;
|
||||
|
||||
public interface ItemInterface {
|
||||
void updateQuality();
|
||||
}
|
||||
@ -6,14 +6,7 @@ package com.gildedrose;
|
||||
* Quality for an item is never negative
|
||||
* Quality for an item is not greater than the constant MAX_QUAILITY_FOR_AN_ITEM
|
||||
*/
|
||||
public class NormalItem {
|
||||
public static final int MAX_QUAILITY_FOR_AN_ITEM = 50;
|
||||
public Item item;
|
||||
|
||||
public NormalItem() {
|
||||
|
||||
}
|
||||
|
||||
public class NormalItem extends BaseItem implements ItemInterface {
|
||||
public NormalItem(Item item) {
|
||||
this.item = item;
|
||||
}
|
||||
@ -25,36 +18,4 @@ public class NormalItem {
|
||||
decreaseQualityBy(1);
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean itemHasExpired() {
|
||||
boolean condition;
|
||||
if (item.sellIn < 0) {
|
||||
condition = true;
|
||||
} else {
|
||||
condition = false;
|
||||
}
|
||||
return condition;
|
||||
}
|
||||
|
||||
protected void increaseQualityBy(int factor) {
|
||||
item.quality += factor;
|
||||
qualityOfAnItemIsNotMoreThan(MAX_QUAILITY_FOR_AN_ITEM);
|
||||
}
|
||||
|
||||
protected void decreaseQualityBy(int factor) {
|
||||
item.quality -= factor;
|
||||
qualityOfAnItemIsNeverNegative();
|
||||
}
|
||||
|
||||
private void qualityOfAnItemIsNotMoreThan(int limit) {
|
||||
if (item.quality > limit) {
|
||||
item.quality = limit;
|
||||
}
|
||||
}
|
||||
|
||||
private void qualityOfAnItemIsNeverNegative() {
|
||||
if (item.quality < 0) {
|
||||
item.quality = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user