POP vs OOP decision patterns in Swift - Performance Comparison
When choosing between Protocol-Oriented Programming (POP) and Object-Oriented Programming (OOP), it's important to understand how the program's work grows as it runs.
We want to see how the decision pattern affects the speed when the program handles more data or tasks.
Analyze the time complexity of this Swift example using POP and OOP patterns.
protocol Drawable {
func draw()
}
class Circle: Drawable {
func draw() { print("Drawing Circle") }
}
class Square: Drawable {
func draw() { print("Drawing Square") }
}
func renderAll(shapes: [Drawable]) {
for shape in shapes {
shape.draw()
}
}
This code uses a protocol to define a drawing action, then classes implement it. The renderAll function calls draw on each shape.
Look at what repeats when the program runs.
- Primary operation: The loop in
renderAllthat callsdraw()on each shape. - How many times: Once for each shape in the input array.
As the number of shapes grows, the number of draw calls grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 draw calls |
| 100 | 100 draw calls |
| 1000 | 1000 draw calls |
Pattern observation: The work grows directly with the number of shapes. Double the shapes, double the work.
Time Complexity: O(n)
This means the time to draw all shapes grows in a straight line with how many shapes there are.
[X] Wrong: "Using protocols (POP) always makes the program slower than classes (OOP)."
[OK] Correct: Both POP and OOP here call the draw method once per shape, so the time grows the same way. The choice affects design more than speed in this case.
Understanding how your design choice affects performance shows you think about both code quality and speed. This skill helps you write clear, efficient programs that grow well with more data.
What if the draw method itself contained a loop over many points? How would that change the overall time complexity?
Practice
Solution
Step 1: Understand POP and OOP basics
POP uses protocols to define behavior and structs or classes to implement it. OOP uses classes and inheritance to share and override behavior.Step 2: Compare the main focus of each paradigm
POP emphasizes protocols and value types (structs), while OOP emphasizes classes and inheritance.Final Answer:
POP focuses on protocols and structs, while OOP focuses on classes and inheritance. -> Option AQuick Check:
POP = protocols & structs, OOP = classes & inheritance [OK]
- Thinking POP uses only classes
- Believing OOP does not use protocols
- Assuming POP cannot reuse code
Solution
Step 1: Check protocol and struct syntax
protocol Vehicle { func drive() }; struct Car: Vehicle { func drive() { print("Driving") } } correctly declares a protocol with a method and a struct conforming by implementing that method.Step 2: Identify syntax errors in other options
class Vehicle { func drive() }; struct Car inherits Vehicle { func drive() { print("Driving") } } uses invalid inheritance syntax for struct. protocol Vehicle { func drive() }; struct Car: Vehicle { var speed: Int = 0 } misses method requirement. protocol Vehicle { func drive() }; class Car: Vehicle { func drive() { print("Driving") } } uses class instead of struct.Final Answer:
protocol Vehicle { func drive() }; struct Car: Vehicle { func drive() { print("Driving") } } -> Option CQuick Check:
Protocol + struct with method implementation = protocol Vehicle { func drive() }; struct Car: Vehicle { func drive() { print("Driving") } } [OK]
- Using 'inherits' keyword with structs
- Not implementing all protocol methods
- Confusing class and struct declarations
protocol Greetable {
func greet() -> String
}
class Person: Greetable {
func greet() -> String {
"Hello from Person"
}
}
class Employee: Person {
override func greet() -> String {
"Hello from Employee"
}
}
let p: Greetable = Person()
let e: Greetable = Employee()
print(p.greet())
print(e.greet())Solution
Step 1: Understand protocol conformance and inheritance
Person conforms to Greetable with greet() returning "Hello from Person". Employee inherits Person and overrides greet() to return "Hello from Employee".Step 2: Analyze output of print statements
Variable p is Person instance, so p.greet() returns "Hello from Person". Variable e is Employee instance, so e.greet() returns "Hello from Employee".Final Answer:
Hello from Person Hello from Employee -> Option AQuick Check:
Inheritance override works, prints Person then Employee [OK]
- Assuming protocol type erases override
- Confusing struct and class inheritance
- Expecting compilation error for override
protocol Drawable {
func draw()
}
class Shape: Drawable {
func draw() {
print("Drawing shape")
}
}
struct Circle: Shape {
func draw() {
print("Drawing circle")
}
}Solution
Step 1: Check inheritance rules for structs and classes
In Swift, structs cannot inherit from classes. Circle is a struct trying to inherit from class Shape, which is invalid.Step 2: Verify protocol conformance and method implementation
Shape correctly conforms to Drawable. Circle tries to override draw(), but inheritance is invalid for structs.Final Answer:
Struct cannot inherit from a class; Circle cannot inherit from Shape. -> Option DQuick Check:
Structs cannot inherit classes in Swift [OK]
- Thinking structs can inherit classes
- Confusing protocol conformance with inheritance
- Using 'override' in struct methods
Solution
Step 1: Analyze design goals
The goal is to share behavior, have unique data, and avoid deep inheritance chains for flexibility.Step 2: Match goals to programming paradigms
POP uses protocols and structs to share behavior flexibly without inheritance. OOP with deep inheritance is less flexible and can be complex.Final Answer:
Use Protocol-Oriented Programming with protocols and structs to share behavior and data. -> Option BQuick Check:
Flexible behavior sharing = POP with protocols & structs [OK]
- Choosing deep inheritance causing rigid design
- Ignoring protocols for behavior sharing
- Using global functions losing type safety
Ready to prove it?
Pass the test and this pattern moves unknown
