- Mastering macOS Programming
- Stuart Grimshaw
- 152字
- 2021-07-02 22:54:27
Protocol-oriented programming
An alternative to creating a class from which subclasses are derived is to define a protocol which any class can adopt. The protocol defines a set of behaviors to which a class, once it has declared itself to be compliant to that protocol, must implement. Thus, a Car object that adopts the Steering protocol will be required to implement a turnLeft method, which can be called by another object that has no need to know what that method involves. A Steering-protocol-compliant Motorbike object will have a different implementation of turnLeft, which again, the calling object need know nothing of.
We will be seeing much more of protocols later on, but for an entertaining and enlightening take on protocol-oriented programming, here is a video of a brilliant session from the 2015 World Wide Developers Conference: https://developer.apple.com/videos/play/wwdc2015/408/.
It's possibly the most amusing Apple video you'll ever see (you'll need to use Safari).