Protocol and protocol extensions compared with superclasses

In the object-oriented programming example, we created a Vehicle superclass from which all the vehicle classes were derived from. In the protocol-oriented programming example, we used a combination of protocols and protocol extensions to achieve the same result; however, there are several advantages to the protocol-oriented design.

To refresh our memory of the two solutions, let's look at the code for both the Vehicle superclass and the Vehicle protocol and protocol extension. The following code shows the Vehicle superclass:

class Vehicle { fileprivate var vehicleTypes = [TerrainType]() fileprivate var vehicleAttackTypes = [TerrainType]() fileprivate var vehicleMovementTypes = [TerrainType]() ...

Get Swift 4 Protocol-Oriented Programming - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.