In the previous recipe, we could create a generic code, which can be used in our future programs, but we have to remember that priority queues are only one kind of queue. It is good practice to define an interface for this abstract data type, and after this, have different implementations.
As you know, in Swift, we have protocols for cases such as this; however, we have a problem; protocols don't have generics. What is the solution? The answer is associated types.
Copy the project of the previous recipe and name it Chapter 3 Flight Protocol
, and create a new file called Queue.swift
.
To create a protocol for the priority queue, follow these steps: