Join Scott Gardner for an in-depth discussion in this video Understand protocol-oriented programming, part of Swift 3 Essential Training: Beyond the Basics.
- [Narrator] In traditional object-oriented programming, if I were going to create classes to represent different types of vehicles for Land, Water, and Air, I would end up with a complex, rigid, hierarchy, like this. And where would I add a HybridCar? Or for that matter, a car that also can travel on water, or fly? I need to create them as subclasses, even though they would have some of the same properties as other subclasses. If you've ever worked on an App for a period of time, especially if you're on a team, you know that this is a recipe for problems as changes may be made to one, but not all duplicate properties or methods across subclasses over time.
Swift is a Single inheritance language, meaning a class can only inherit from one superclass, which is definitely a good thing in my book because Multi-inheritance can lead to even more complexity than in a Single inheritance environment. The solution is to go for simplicity, abstraction, and modularity, and that is the basis of Protocol-Oriented Programming. Protocols define requirements and set reliable expectations, and by extending protocols, which I'll show you later in this chapter, you can provide a default implementation and share logic across multiple types that conform to a protocol.
This enables writing code that is expressive, modular, and safe. Swift is predominantly a protocol-driven language. If I were to re-implement the hierarchical inheritance tree I showed you earlier, purely using protocols instead, I would define multiple protocols that independently define requirements. And then I would define each type, adopting and conforming to the necessary protocols straightaway without having to navigate a complex, rigid hierarchy. And adding a HybridCar, AmphiCar, and FlyingCar would be just as simple.
The truth is though, I would probably implement this using a combination of mostly protocols and some subclassing. After all, it's Protocol-Oriented Programming, not protocol mandated, and subclassing still has a place in good software architecture.
- Adding source files, resources, links, and literals
- Adding pages to a playground
- Using overflow operators and bitwise operators
- Using ranges with strings
- Creating complex sequences
- Chaining higher-order functions
- Defining lazy properties
- Using failable initializers
- Mutating methods
- Working with singletons
- Nesting function types
- Creating error types and recursive enumerations
- Extending concrete types
- Referencing selectors and key paths
- Working with protocol-oriented programming
- Defining class-only protocols and optional protocols
- Using option sets, type checking, and casting operators
Skill Level Intermediate
Core Data for iOS and macOS Enterprise Developerswith Jon Bott1h 46m Intermediate
Distributing Your iOS App for Testing with TestFlightwith Ron Buencamino35m 10s Intermediate
iOS App Development: Test-Driven Developmentwith Harrison Ferrone2h 14m Intermediate
3. Strings and Characters
4. Sequences and Collections
5. Control Flow
6. Value and Reference Types
9. Access Control
Understand access control3m 47s
Next steps1m 2s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.