Goal: I am learning about SOLID principles and trying to refactor Gilded Rose code, in order to make it fresh and clean.
What I have done: I have created an AbstractItem
class as follows, which "wraps" Item
class:
public abstract class AbstractItem {
protected static final int MIN_QUALITY = 0;
protected static int MAX_QUALITY = 50;
public Item item; // name, quality and sellIn
public AbstractItem(Item item) {
this.item = item;
}
public abstract void updateQuality();
public boolean isValid(int quality) {
return quality > MIN_QUALITY && quality < MAX_QUALITY;
}
public void updateSellIn() {
item.sellIn--;
}
public boolean hasExpired() {
return item.sellIn < 0;
}
public int getRate() {
return hasExpired() ? 2 : 1;
}
}
and it gets extended by AgedBrie, Sulfuras, BackstagePasses and any other item which is sold by the Gilded Rose. For example, if Gilded Rose will sell MagicApples
in 2050, I will define the MagicApples
class which extends AbstractItem
.
My doubt: I was wondering whether this class violates the SRP (or any other SOLID principles) or you think this is ugly or not:
public class BackstagePasses extends AbstractItem {
public BackstagePasses(Item item) {
super(item);
}
@Override
public void updateQuality() {
if (isValid(item.quality)) {
if (hasExpired())
item.quality = 0;
else
item.quality += getRate();
}
}
@Override
public int getRate() {
int daysToConcert = item.sellIn;
// should I refactor the following statements?
if (daysToConcert <= 5)
return 3;
else if (daysToConcert <= 10)
return 2;
else
return 1;
}
}
Thank you for your advice and patience.