Skip to content

Instantly share code, notes, and snippets.

@LeeKahSeng
Last active September 27, 2017 09:12
Show Gist options
  • Save LeeKahSeng/85ab38a39dd68d8dcff4ecb1d5861956 to your computer and use it in GitHub Desktop.
Save LeeKahSeng/85ab38a39dd68d8dcff4ecb1d5861956 to your computer and use it in GitHub Desktop.
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