Introduction
Protocol-oriented programming (POP) is a design philosophy in Swift, famously promoted at WWDC 2015 in the talk "Protocol-Oriented Programming in Swift," that favors composing behavior from small, focused protocols rather than building deep class inheritance hierarchies. A key enabler of POP is protocol extensions, which let you provide a default implementation for a protocol's methods so that every conforming type gets that behavior for free, unless it chooses to override it.
Cricket analogy: Rather than forcing every player through one rigid 'Cricketer' class hierarchy, POP is like giving small skill badges — Batter, Bowler, Fielder — that any player can adopt, echoing how WWDC 2015 promoted composing behavior from focused capabilities instead of deep inheritance.
Syntax
protocol Greetable {
var name: String { get }
func greet()
}
extension Greetable {
func greet() {
print("Hello, \(name)!")
}
}Explanation
Here Greetable declares two requirements, name and greet(). The extension on Greetable supplies a default implementation of greet() that uses the conforming type's name property. Any type that adopts Greetable automatically gets this default greet() behavior without writing any code, but it can still override greet() with its own implementation if it needs different behavior. This pattern lets many unrelated structs, classes, and enums share behavior through composition of small protocols, instead of being forced into a single rigid class hierarchy.
Cricket analogy: Greetable is like a 'Fielder' certification requiring a position name and a default catching technique; any player adopting it gets the standard technique for free, like inheriting a default slip-catching style, but a specialist can still override it with their own diving style.
Example
protocol Greetable {
var name: String { get }
func greet()
}
extension Greetable {
func greet() {
print("Hello, \(name)!")
}
}
struct Visitor: Greetable {
var name: String
}
let guest = Visitor(name: "Ava")
guest.greet()Output
Hello, Ava!Key Takeaways
- Protocol-oriented programming favors composing small protocols over deep class inheritance.
- Protocol extensions can provide default implementations that all conforming types receive automatically.
- Conforming types can override a default implementation with their own custom behavior.
- POP works across structs, classes, and enums, not just classes as with traditional inheritance.
- This approach was popularized by Apple's WWDC 2015 talk on Protocol-Oriented Programming in Swift.
Practice what you learned
1. What Apple event popularized protocol-oriented programming in Swift?
2. What feature allows protocols to provide default method implementations?
3. Can a conforming type override a default implementation provided by a protocol extension?
4. How does protocol-oriented programming primarily differ from traditional object-oriented programming in Swift?
Was this page helpful?
You May Also Like
Protocols in Swift
Learn how Swift protocols define blueprints of methods and properties that structs, classes, and enums can conform to.
Extensions in Swift
Discover how Swift extensions add new functionality to existing types without modifying their original source code.
Structs vs Classes in Swift
Structs use value semantics while classes use reference semantics — the defining choice you make for every custom Swift type.
Inheritance in Swift
Inheritance lets a class build on a superclass's properties and methods, overriding behavior with the override keyword.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics