CodeX
Published in

CodeX

Why you should be using Protocols.

Photo by Alex Bachor on Unsplash
protocol Routine {
wakeUp()
getCoffee()
goToWork()
}
class MondayRoutine: Routine {
func wakeUp() {
print("...turning off alarm")
}
func getCoffee() {
print("Grabbing a cup of coffee")
}
func goToWork() {
print("Driving to work")
}
}
class TuesdayRoutine: Routine {
func wakeUp() {
print("...overslept alarm")
}
func getCoffee() {
print("Pour two cups of coffee")
}
func goToWork() {
print("Take the bus to work")
}
}
func weeklyRoutine(days: [Routine]) {
for day in days {
day.wakeUp()
day.getCoffee()
day.goToWork()
}
}

--

--

Everything connected with Tech & Code. Follow to join our 1M+ monthly readers

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store