REFACTOR: introduce strategy pattern for item types
This commit is contained in:
parent
5655326543
commit
7a17790915
1 changed files with 44 additions and 22 deletions
|
|
@ -11,26 +11,14 @@ class Item {
|
|||
String toString() => '$name, $sellIn, $quality';
|
||||
}
|
||||
|
||||
class GildedRose {
|
||||
List<Item> items;
|
||||
|
||||
GildedRose(this.items);
|
||||
|
||||
void updateQuality() {
|
||||
for (final item in items) {
|
||||
if (item.name == 'Sulfuras, Hand of Ragnaros') {
|
||||
_updateSulfuras(item);
|
||||
} else if (item.name == 'Aged Brie') {
|
||||
_updateAgedBrie(item);
|
||||
} else if (item.name == 'Backstage passes to a TAFKAL80ETC concert') {
|
||||
_updateBackstagePasses(item);
|
||||
} else {
|
||||
_updateNormalItem(item);
|
||||
}
|
||||
}
|
||||
/// Strategy pattern: Each item type has its own update behavior
|
||||
abstract class ItemUpdater {
|
||||
void update(Item item);
|
||||
}
|
||||
|
||||
void _updateNormalItem(Item item) {
|
||||
class NormalItemUpdater implements ItemUpdater {
|
||||
@override
|
||||
void update(Item item) {
|
||||
// Quality decreases by 1 each day
|
||||
if (item.quality > 0) {
|
||||
item.quality -= 1;
|
||||
|
|
@ -43,8 +31,11 @@ class GildedRose {
|
|||
item.quality -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _updateAgedBrie(Item item) {
|
||||
class AgedBrieUpdater implements ItemUpdater {
|
||||
@override
|
||||
void update(Item item) {
|
||||
// Quality increases as it ages
|
||||
if (item.quality < 50) {
|
||||
item.quality += 1;
|
||||
|
|
@ -57,8 +48,11 @@ class GildedRose {
|
|||
item.quality += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _updateBackstagePasses(Item item) {
|
||||
class BackstagePassUpdater implements ItemUpdater {
|
||||
@override
|
||||
void update(Item item) {
|
||||
// Quality increases as concert approaches
|
||||
if (item.quality < 50) {
|
||||
item.quality += 1;
|
||||
|
|
@ -81,9 +75,37 @@ class GildedRose {
|
|||
item.quality = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _updateSulfuras(Item item) {
|
||||
class SulfurasUpdater implements ItemUpdater {
|
||||
@override
|
||||
void update(Item item) {
|
||||
// Legendary item never changes
|
||||
// Quality always 80, sellIn never decreases
|
||||
}
|
||||
}
|
||||
|
||||
class GildedRose {
|
||||
List<Item> items;
|
||||
|
||||
GildedRose(this.items);
|
||||
|
||||
void updateQuality() {
|
||||
for (final item in items) {
|
||||
final updater = _selectUpdater(item);
|
||||
updater.update(item);
|
||||
}
|
||||
}
|
||||
|
||||
ItemUpdater _selectUpdater(Item item) {
|
||||
if (item.name == 'Sulfuras, Hand of Ragnaros') {
|
||||
return SulfurasUpdater();
|
||||
} else if (item.name == 'Aged Brie') {
|
||||
return AgedBrieUpdater();
|
||||
} else if (item.name == 'Backstage passes to a TAFKAL80ETC concert') {
|
||||
return BackstagePassUpdater();
|
||||
} else {
|
||||
return NormalItemUpdater();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue