Protocols
The first tool we will look at is protocols. A protocol is essentially a contract that a type can sign saying that it will provide a certain interface to other components. This relationship is significantly weaker than the relationship a subclass has with its superclass. A protocol does not have its own implementation. Instead, a type can implement the protocol in any way they like.
Let's take a look at how we define a protocol so that we can understand them better.
Defining a protocol
Let's say we have some code that needs to interact with a collection of strings. We don't actually care what order they are stored in and we only need to be able to add and enumerate the elements inside the container. One option to do this would be to simply use an array, but an array does way more than we need it to. What if we decide later that we would rather write and read the elements from the filesystem? Furthermore, what if we want to write a container that would intelligently...