Protocol-Oriented Programming in Swift: Embracing a New Paradigm

KD Knowledge Diet
3 min readJan 24, 2024

Swift introduced a paradigm shift in how developers approach programming tasks with its strong emphasis on protocol-oriented programming (POP). This approach leverages the power of protocols to define blueprints of methods, properties, and other requirements for particular tasks. Swift’s protocols are more than just interfaces; they’re a fundamental aspect of the language’s design philosophy. Let’s delve into the world of protocol-oriented programming and see how it shapes the way we build applications in Swift.

What is Protocol-Oriented Programming?

Protocol-oriented programming is a design paradigm in Swift that recommends using protocols and protocol extensions as the primary tool for abstraction and code organization. Unlike class-based object-oriented programming, which is centered around inheritance hierarchies, POP focuses on defining interfaces and behaviors that can be adopted by any type.

The Power of Protocols in Swift

Protocols in Swift define a blueprint of methods, properties, and other requirements. They are similar to interfaces in other languages but with supercharged capabilities:

  1. Protocols can be adopted by classes, structs, and enums, which isn’t possible with class inheritance.
  2. Protocol extensions allow you to provide default implementations for the methods and behaviors defined in protocols.
  3. Protocols can be composed together, allowing for more flexible and reusable code without the drawbacks of multiple inheritances.

Why Choose POP?

  1. Increased Flexibility: Protocols can be adopted by any type, making them more flexible than base classes.
  2. Reusability: Protocol extensions can provide default functionality that can be reused by multiple types.
  3. Avoiding Inheritance Problems: POP helps avoid issues like tight coupling and fragility that can arise from classical inheritance.
  4. Enhanced Safety: Protocols make your intentions explicit, leading to safer, more predictable code.

Protocol Extensions: A Key Tool

Protocol extensions in Swift are what set POP apart from other programming paradigms. They let you extend the functionality of a protocol and provide a default implementation that conforming types can use out-of-the-box or override as needed.

protocol Greetable {
func greet()
}

extension Greetable {
func greet() {
print("Hello, there!")
}
}
struct Person: Greetable {
// This type automatically gains the default `greet` implementation
}
let john = Person()
john.greet() // Prints "Hello, there!"

Embracing POP in Your Swift Code

  1. Start with Protocols: When designing your system, start by defining protocols for the core functionalities and behaviors you need.
  2. Use Protocol Extensions: Implement common functionalities using protocol extensions to provide default behavior.
  3. Prefer Composition Over Inheritance: Combine protocols to build up more complex behaviors while keeping types decoupled.
  4. Leverage Value Types: Swift’s structs and enums are first-class citizens and work seamlessly with protocols, often providing better performance and safety than classes.

POP in Action: An Example

Consider a game where you have different characters that can move. Instead of creating a base class for all characters, you can define a protocol and extend it:

protocol Moveable {
func move()
}

extension Moveable {
func move() {
print("Moving forward...")
}
}
struct Knight: Moveable {
// Inherits the default move behavior
}
struct Archer: Moveable {
// You can provide a custom implementation if needed
func move() {
print("Moving silently...")
}
}

let knight = Knight()
let archer = Archer()
knight.move() // Prints "Moving forward..."
archer.move() // Prints "Moving silently..."

Conclusion

Protocol-oriented programming is not just a feature of Swift, but a guiding principle of its design. By focusing on protocols and their extensions, Swift developers can write code that’s more modular, reusable, and expressive. As Swift continues to evolve, the prominence of POP does as well, paving the way for a more robust and flexible way to build applications. Whether you’re a seasoned Swift developer or just starting, embracing the principles of protocol-oriented programming can lead to better software design and implementation.

--

--

KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!