Last active
September 27, 2017 09:12
-
-
Save LeeKahSeng/85ab38a39dd68d8dcff4ecb1d5861956 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Flyable { | |
// Make speedLimit only gettable | |
var speedLimit: Int { get } | |
func fly() | |
} | |
class Bird: Flyable { | |
// Make speedLimit private | |
private(set) var speedLimit = 20 | |
} | |
class SpeedLimitUpdater { | |
static func reduceSpeedLimit(of flyables: [Flyable], by value: Int) { | |
// Only update speedLimit of Helicopter & Airplane | |
for flyable in flyables { | |
if let helicopter = flyable as? Helicopter { | |
helicopter.speedLimit -= value | |
} else if let airplane = flyable as? Airplane { | |
airplane.speedLimit -= value | |
} | |
} | |
} | |
static func increaseSpeedLimit(of flyables: [Flyable], by value: Int) { | |
// Only update speedLimit of Helicopter & Airplane | |
for flyable in flyables { | |
if let helicopter = flyable as? Helicopter { | |
helicopter.speedLimit += value | |
} else if let airplane = flyable as? Airplane { | |
airplane.speedLimit += value | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment