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