mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 14:31:28 +00:00
- Code refactoring as per SOLID Principles
This commit is contained in:
parent
bf62bf2ceb
commit
08c1415e17
@ -7,16 +7,10 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
protocol CustomisedItem {
|
||||
var item: Item {get set}
|
||||
func updateItemState()
|
||||
}
|
||||
|
||||
protocol CustomisedItemFactoryCreator {
|
||||
static func getCustomisedItem(item: Item) -> CustomisedItem
|
||||
}
|
||||
|
||||
|
||||
struct CustomisedItemFactory: CustomisedItemFactoryCreator {
|
||||
// Returns the Customised Item based on the Item name
|
||||
static func getCustomisedItem(item: Item) -> CustomisedItem {
|
||||
|
||||
@ -17,4 +17,3 @@ struct ValueConstants {
|
||||
static let kHightestQualityValue = 50
|
||||
static let kLowestQualityValue = 0
|
||||
}
|
||||
|
||||
|
||||
@ -11,14 +11,7 @@ protocol ItemQualityUpdater: CustomisedItem {
|
||||
}
|
||||
|
||||
extension ItemQualityUpdater {
|
||||
var isItemUnderHighestQuality: Bool {
|
||||
return item.quality < ValueConstants.kHightestQualityValue
|
||||
}
|
||||
|
||||
var isItemMoreThanLowestQuality: Bool {
|
||||
return item.quality > ValueConstants.kLowestQualityValue
|
||||
}
|
||||
|
||||
|
||||
func reduceItemQuality(by value:Int) {
|
||||
item.quality -= value
|
||||
}
|
||||
@ -33,4 +26,3 @@ extension ItemQualityUpdater {
|
||||
}
|
||||
|
||||
typealias ItemStateUpdater = ItemSellInUpdater & ItemQualityUpdater
|
||||
|
||||
|
||||
@ -12,9 +12,6 @@ protocol ItemSellInUpdater: CustomisedItem{
|
||||
}
|
||||
|
||||
extension ItemSellInUpdater {
|
||||
var isSellInDatePassed: Bool{
|
||||
return item.sellIn < 0
|
||||
}
|
||||
func reduceSellInDays(by days: Int) {
|
||||
item.sellIn -= days
|
||||
}
|
||||
|
||||
@ -9,7 +9,11 @@ import Foundation
|
||||
|
||||
struct AgedBrieItem: ItemStateUpdater{
|
||||
var item: Item
|
||||
|
||||
|
||||
private var isItemUnderHighestQuality: Bool {
|
||||
return item.quality < ValueConstants.kHightestQualityValue
|
||||
}
|
||||
|
||||
public init(item: Item) {
|
||||
self.item = item
|
||||
}
|
||||
@ -22,7 +26,4 @@ struct AgedBrieItem: ItemStateUpdater{
|
||||
guard isItemUnderHighestQuality else { return }
|
||||
increaseItemQuality(by: 1)
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -10,6 +10,14 @@ import Foundation
|
||||
struct BackstagePassesItem: ItemStateUpdater {
|
||||
var item: Item
|
||||
|
||||
private var isItemUnderHighestQuality: Bool {
|
||||
return item.quality < ValueConstants.kHightestQualityValue
|
||||
}
|
||||
|
||||
private var isSellInDatePassed: Bool{
|
||||
return item.sellIn < 0
|
||||
}
|
||||
|
||||
public init(item: Item) {
|
||||
self.item = item
|
||||
}
|
||||
|
||||
13
swift/Sources/GildedRose/Items/CustomisedItem.swift
Normal file
13
swift/Sources/GildedRose/Items/CustomisedItem.swift
Normal file
@ -0,0 +1,13 @@
|
||||
//
|
||||
// File.swift
|
||||
//
|
||||
//
|
||||
// Created by Manali Mogre on 17/08/2020.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
protocol CustomisedItem {
|
||||
var item: Item {get set}
|
||||
func updateItemState()
|
||||
}
|
||||
@ -10,6 +10,21 @@ import Foundation
|
||||
struct StandardItem: ItemStateUpdater {
|
||||
var item: Item
|
||||
|
||||
private var isSellInDatePassed: Bool{
|
||||
return item.sellIn < 0
|
||||
}
|
||||
|
||||
var decreasingValueOverZeroDaysToSell: Int {
|
||||
return 1
|
||||
}
|
||||
private var decreasingValueForZeroOrLessDaysToSell: Int {
|
||||
return 2 * decreasingValueOverZeroDaysToSell
|
||||
}
|
||||
|
||||
private var isItemMoreThanLowestQuality: Bool {
|
||||
return item.quality > ValueConstants.kLowestQualityValue
|
||||
}
|
||||
|
||||
public init(item: Item) {
|
||||
self.item = item
|
||||
}
|
||||
@ -17,9 +32,9 @@ struct StandardItem: ItemStateUpdater {
|
||||
func updateItemState() {
|
||||
// Reduce the sellIn days for Item by 1
|
||||
reduceSellInDays(by: 1)
|
||||
|
||||
|
||||
// Reduce the item quality by 1 , if the sell in date is passed decrement by double the value
|
||||
isSellInDatePassed ? reduceItemQuality(by: decreasingValueForZeroOrLessDaysToSell()) : reduceItemQuality(by: decreasingValueOverZeroDaysToSell())
|
||||
isSellInDatePassed ? reduceItemQuality(by: decreasingValueForZeroOrLessDaysToSell) : reduceItemQuality(by: decreasingValueOverZeroDaysToSell)
|
||||
|
||||
guard isItemMoreThanLowestQuality else {
|
||||
// Sets the quality to zero if the quality is negative
|
||||
@ -27,12 +42,4 @@ struct StandardItem: ItemStateUpdater {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func decreasingValueOverZeroDaysToSell() -> Int {
|
||||
return 1
|
||||
}
|
||||
|
||||
private func decreasingValueForZeroOrLessDaysToSell() -> Int {
|
||||
return 2 * decreasingValueOverZeroDaysToSell()
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user