Why protocol-oriented design matters in Swift - Performance Analysis
When using protocol-oriented design in Swift, it is important to understand how the program's running time changes as the input grows.
We want to know how the use of protocols affects the speed of our code.
Analyze the time complexity of the following code snippet.
protocol Drawable {
func draw()
}
struct Circle: Drawable {
func draw() {
print("Drawing Circle")
}
}
func drawAll(shapes: [Drawable]) {
for shape in shapes {
shape.draw()
}
}
This code defines a protocol and a struct that conforms to it, then draws all shapes in a list by calling their draw method.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the array of shapes and calling draw() on each.
- How many times: Once for each shape in the input array.
As the number of shapes increases, the number of draw calls grows directly with it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 draw calls |
| 100 | 100 draw calls |
| 1000 | 1000 draw calls |
Pattern observation: The work grows in a straight line with the number of shapes.
Time Complexity: O(n)
This means the time to draw all shapes grows directly with how many shapes there are.
[X] Wrong: "Using protocols makes the code slower because of extra overhead in method calls."
[OK] Correct: Protocol calls in Swift are efficient, and the main time cost comes from how many times you call the method, not the protocol itself.
Understanding how protocol-oriented design affects performance helps you write clean code without worrying about hidden slowdowns.
"What if we added nested loops inside the draw() method? How would the time complexity change?"
Practice
Solution
Step 1: Understand what protocols do
Protocols define a set of behaviors that many types can adopt and share.Step 2: Identify the main benefit
This sharing of behavior helps make code reusable and flexible.Final Answer:
They allow different types to share common behavior. -> Option DQuick Check:
Protocols = shared behavior [OK]
- Thinking protocols speed up code execution
- Confusing protocols with classes
- Believing protocols store data
Solution
Step 1: Recall Swift protocol syntax
Protocols are declared using the keyword 'protocol' followed by the name and curly braces.Step 2: Match the correct syntax
protocol Vehicle { func drive() } correctly uses 'protocol Vehicle { func drive() }'. Other options misuse keywords or structure.Final Answer:
protocol Vehicle { func drive() } -> Option CQuick Check:
Protocol declaration = protocol keyword + name + braces [OK]
- Using 'class' instead of 'protocol'
- Trying to inherit protocol with ':' in struct incorrectly
- Writing 'func protocol' which is invalid
protocol Greetable { func greet() -> String }
struct Person: Greetable {
func greet() -> String { "Hello!" }
}
let p = Person()
print(p.greet())What will be the output?
Solution
Step 1: Check protocol conformance
Person struct implements the greet() method required by Greetable.Step 2: Understand the print output
Calling p.greet() returns "Hello!", which is printed.Final Answer:
Hello! -> Option AQuick Check:
Implemented method returns "Hello!" [OK]
- Assuming protocol methods print their name
- Expecting runtime error without implementation
- Thinking print outputs nil if no return
protocol Drawable {
func draw()
}
struct Circle: Drawable {
// Missing draw() implementation
}
let c = Circle()Solution
Step 1: Check protocol requirements
Drawable requires any conforming type to implement draw().Step 2: Verify Circle implementation
Circle does not implement draw(), so it violates the protocol.Final Answer:
Circle must implement draw() or code won't compile. -> Option AQuick Check:
Protocol methods must be implemented [OK]
- Thinking structs can skip protocol methods
- Believing protocols can't have functions
- Confusing struct vs class requirements
Car and Bicycle can be "startable" with a start() method. Using protocol-oriented design, which approach is best?Solution
Step 1: Identify shared behavior
Both Car and Bicycle need a start() method, which is shared behavior.Step 2: Apply protocol-oriented design
Defining a Startable protocol with start() and making both types conform promotes reuse and flexibility.Step 3: Compare other options
Subclassing limits flexibility; separate functions or globals reduce clarity and reuse.Final Answer:
Create a protocol Startable with start() and have both types conform to it. -> Option BQuick Check:
Protocol for shared behavior = best design [OK]
- Using class inheritance when unrelated types
- Writing separate functions instead of protocol
- Using global state instead of behavior abstraction
Ready to prove it?
Pass the test and this pattern moves unknown
