I ask a question today because I’m little bit lost today. It’s about Swift and protocol and more about Protocol Oriented Programming (POP).
I read articles about it, even a book but I’m still confused. Everybody seems to say that Protocol is a great tool and so on but I don’t really understand its power.
I’ve a question because I’m coding a class Volume which is the representation of a volume as an object. Let’s say
struct Volume {
var value: Float = 1
var isLogScale: Bool = false
var maxLogVolume: Float = 6.0 // value in dB
var maxLinearVolume: Float = 1.0
let dynamicRange: Float = 50.0
func convertLinearToLog() -> Float {
// Do some maths
}
func otherFunction() {
print("Hello, I'm the most useless function in the world.")
}
}
This is a typical class nothing special.
But… Should I better us a protocol like this:
protocol VolumeProtocol {
var value: Float {get set}
var isLogScale: Bool {get set}
var maxLogVolume: Float {get set}
var maxLinearVolume: Float {get set}
let dynamicRange: Float {get}
func convertLinearToLog() -> Float
func otherFunction()
}
And then re implement everything like
struct MyVolumeClass: VolumeProtocol {
// Same thing as before
}
I don’t really manage to answer to this question, so if you could help me when to use protocol and when not I would appreciate.
Protocols have more than one use case, but think of it this way:
For classes, protocols provide a lightweight inheritance hierarchy separate from the class hierarchy. Given a class Animal and its subclasses Cat, Dog, Bird, and Insect, how would you specify that only Bird and Insect share the fly
method?
For structs, protocols provide an inheritance hierarchy that would otherwise be missing entirely! A struct has no superstruct. So, given a struct Bird and a struct Insect, how would you specify that they share a fly
method?
Now, you could answer that Bird and Insect just happen to have fly
methods and that's the end of the story. But that won't do when you need to speak of the set "all types that have a fly
method". And you do need to speak of that set when you want the compiler to be able to send the fly
method to an object on the grounds that it has a fly
method.
The solution is a protocol:
protocol Flier {
func fly()
}
Now Flier is a type. An instance of any type that conforms to Flier can be used where a Flier is expected, and the compiler will let you tell any Flier to fly
, so the problem is solved:
var myFlier : Flier = Bird() // if Bird conforms to Flier
myFlier.fly() // legal
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With