Chapter 4. All about the Protocol

Coming from an object-oriented background, I am very familiar with protocols (or interfaces, as they are known in other object-oriented languages); however, prior to Apple introducing protocol-oriented programming, protocols or interfaces were rarely the focal point of my application designs unless I was working with an OSGI-based project. When I designed an application in an object-oriented way, I always began the design with the objects. The protocols or interfaces were then used where they made sense mainly for polymorphism when a class hierarchy did not make sense. Now all that has changed and with protocol-oriented programming the protocol has been elevated to the focal point of our application design.

In this chapter you will learn:

When we are designing an application in an object-oriented way, we begin the design by focusing on the objects and how they interact. The object is a data structure that contains information about the attributes of the object in the form of properties and the actions performed by or to the object in the form of methods. We cannot create an object without a blueprint that tells the application what attributes and actions to expect from the object. In most object-oriented languages, this blueprint comes in the form of a class. A class is a construct that allows us to encapsulate the properties and actions of the object into a single type.

Most object-oriented programming languages contain an interface type. This interface is a type that contains method and property signatures but does not contain any implementation details. An interface can be considered a contract where any type that conforms to the interface must implement the required functionality defined within the interface.

Interfaces in most object-oriented languages are primarily used as a way to achieve polymorphism. There are some frameworks, such as OSGI, that use interfaces extensively; however in most object-oriented designs the interface takes a back seat to the class and class hierarchy.

Designing an application in a protocol-oriented way is significantly different from designing it in an object-oriented way. As we stated earlier, object-oriented design begins with the objects and the interaction between the objects while protocol-oriented design begins with the protocol. While protocol-oriented design is about so much more than just the protocol, we can think of the protocol as the backbone of protocol-oriented programming. After all, it would be pretty hard to have protocol-oriented programming without the protocol.

A protocol in Swift is similar to interfaces in object-oriented languages where the protocol acts as a contract that defines the methods, properties, and other requirements needed by our types to perform their task. We say that the protocol acts as a contract because any type that adopts, or conforms, to the protocol promises to implement the requirements defined by the protocol.

Any class, structure, or enumeration can conform to a protocol. A type cannot conform to a protocol unless it implements all required functionality defined within the protocol. If a type adopts a protocol and it does not implement all functionality defined by the protocol, we will get a compile time error and the project will not compile.

Most modern object-oriented programming languages implement their standard library with a class hierarchy; however, the basis of the Swift's standard library is the protocol. (https://developer.apple.com/library/prerelease/ios/documentation/General/Reference/SwiftStandardLibraryReference/index.html). Therefore, not only does Apple recommend that we use the protocol-oriented programming paradigm in our applications, but they also use it in the Swift standard library.

With the protocol being the basis of the Swift standard library and also the backbone of the protocol-oriented programming paradigm, it is very important that we fully understand what the protocol is and how we can use it. In this chapter we will go over the basic usage of the protocol, which will include the syntax for defining the protocol, how to define requirements in a protocol, and how to make our types conform to a given protocol. At the end of this chapter, we will look at how to implement the delegation pattern in Swift using protocols and how to use protocol composition to create small, very specific protocols rather than large monolithic ones.

In this section we will look at how to define a protocol, define requirements within a protocol, and specify that a type conforms to a protocol. We will start off by seeing how we define a protocol.