Bird
Raised Fist0
Swiftprogramming~10 mins

POP vs OOP decision patterns in Swift - Visual Side-by-Side Comparison

Choose your learning style10 modes available
Concept Flow - POP vs OOP decision patterns
Start: Need to model behavior
↓
Decide: Use OOP or POP?
↓
OOP: Classes
↓
Use inheritance
↓
Override methods
↓
Use polymorphism
↓
Manage shared state
↓
End: Behavior modeled
Start by deciding if you want to use classes (OOP) or protocols and structs (POP). OOP uses inheritance and shared state, POP uses protocol conformance and value types.
Execution Sample
Swift
protocol Vehicle {
    func drive()
}

class Car: Vehicle {
    func drive() { print("Car driving") }
}

struct Bike: Vehicle {
    func drive() { print("Bike driving") }
}
Defines a Vehicle protocol, then a Car class and Bike struct that both implement drive() differently.
Execution Table
StepActionTypeMethod CalledOutput
1Create Car instanceClass--
2Call drive() on CarClassdrive()Car driving
3Create Bike instanceStruct--
4Call drive() on BikeStructdrive()Bike driving
5Assign Car to Vehicle variableClass--
6Assign Bike to Vehicle variableStruct--
7Call drive() on Vehicle (Car)Classdrive()Car driving
8Call drive() on Vehicle (Bike)Structdrive()Bike driving
9End of example---
💡 All instances called their own drive() method, showing polymorphism in OOP and protocol conformance in POP.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5After Step 6Final
carnilCar instanceCar instanceCar instanceCar instanceCar instance
bikenilnilBike instanceBike instanceBike instanceBike instance
vehicleCarnilnilnilCar instanceCar instanceCar instance
vehicleBikenilnilnilnilBike instanceBike instance
Key Moments - 3 Insights
Why does the Car class allow method overriding but the Bike struct does not?
Because Car is a class (OOP) supporting inheritance and method overriding, while Bike is a struct (POP) which is a value type and does not support inheritance.
How does protocol conformance enable polymorphism in both OOP and POP?
Both Car and Bike conform to the Vehicle protocol, so variables typed as Vehicle can hold either type and call drive(), as shown in steps 7 and 8.
Why might you choose POP over OOP for shared state management?
POP prefers value types like structs which avoid shared mutable state, making code safer and easier to reason about compared to OOP classes that share state.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output when calling drive() on the Bike instance at step 4?
A"Car driving"
B"Bike driving"
CNo output
DError
💡 Hint
Check row with Step 4 in execution_table where drive() on Bike is called.
At which step is the Car instance assigned to a variable typed as Vehicle?
AStep 5
BStep 3
CStep 1
DStep 6
💡 Hint
Look for assignment actions in execution_table rows.
If Bike was changed from a struct to a class, which behavior would change?
ABike would lose drive() method
BBike would no longer conform to Vehicle
CBike could then override methods
DNo change at all
💡 Hint
Refer to key_moments about class vs struct differences.
Concept Snapshot
POP vs OOP decision patterns in Swift:
- OOP uses classes with inheritance and shared state.
- POP uses protocols and structs with value types.
- Use OOP for polymorphism via inheritance.
- Use POP for safer, simpler code with protocol conformance.
- Both can achieve polymorphism via protocols.
Full Transcript
This visual execution shows how to decide between Protocol-Oriented Programming (POP) and Object-Oriented Programming (OOP) in Swift. We start by choosing whether to use classes (OOP) or protocols and structs (POP). The example defines a Vehicle protocol with a drive() method. Then a Car class and a Bike struct both implement drive() differently. The execution table traces creating instances, calling drive(), and assigning to Vehicle-typed variables. It shows polymorphism working in both OOP and POP styles. The variable tracker follows instance assignments. Key moments clarify why classes support method overriding but structs do not, how protocol conformance enables polymorphism, and why POP prefers value types to avoid shared state. The quiz tests understanding of outputs, assignment steps, and behavior changes if Bike was a class. The snapshot summarizes the main differences and when to use each pattern.

Practice

(1/5)
1. Which statement best describes the main difference between Protocol-Oriented Programming (POP) and Object-Oriented Programming (OOP) in Swift?
easy
A. POP focuses on protocols and structs, while OOP focuses on classes and inheritance.
B. POP uses only classes, and OOP uses only structs.
C. POP does not support code reuse, but OOP does.
D. OOP uses protocols, but POP does not.

Solution

  1. 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.
  2. Step 2: Compare the main focus of each paradigm

    POP emphasizes protocols and value types (structs), while OOP emphasizes classes and inheritance.
  3. Final Answer:

    POP focuses on protocols and structs, while OOP focuses on classes and inheritance. -> Option A
  4. Quick Check:

    POP = protocols & structs, OOP = classes & inheritance [OK]
Hint: POP = protocols + structs, OOP = classes + inheritance [OK]
Common Mistakes:
  • Thinking POP uses only classes
  • Believing OOP does not use protocols
  • Assuming POP cannot reuse code
2. Which of the following Swift code snippets correctly declares a protocol and a struct conforming to it?
easy
A. protocol Vehicle { func drive() }; struct Car: Vehicle { var speed: Int = 0 }
B. class Vehicle { func drive() }; struct Car inherits Vehicle { func drive() { print("Driving") } }
C. protocol Vehicle { func drive() }; struct Car: Vehicle { func drive() { print("Driving") } }
D. protocol Vehicle { func drive() }; class Car: Vehicle { func drive() { print("Driving") } }

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    protocol Vehicle { func drive() }; struct Car: Vehicle { func drive() { print("Driving") } } -> Option C
  4. Quick Check:

    Protocol + struct with method implementation = protocol Vehicle { func drive() }; struct Car: Vehicle { func drive() { print("Driving") } } [OK]
Hint: Protocols declare methods; structs implement them with correct syntax [OK]
Common Mistakes:
  • Using 'inherits' keyword with structs
  • Not implementing all protocol methods
  • Confusing class and struct declarations
3. What will be the output of the following Swift code?
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())
medium
A. Hello from Person Hello from Employee
B. Hello from Person Hello from Person
C. Hello from Employee Hello from Employee
D. Compilation error due to protocol conformance

Solution

  1. 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".
  2. 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".
  3. Final Answer:

    Hello from Person Hello from Employee -> Option A
  4. Quick Check:

    Inheritance override works, prints Person then Employee [OK]
Hint: Subclass overrides method; output reflects actual instance type [OK]
Common Mistakes:
  • Assuming protocol type erases override
  • Confusing struct and class inheritance
  • Expecting compilation error for override
4. Identify the error in this Swift code that tries to use POP and OOP together:
protocol Drawable {
    func draw()
}

class Shape: Drawable {
    func draw() {
        print("Drawing shape")
    }
}

struct Circle: Shape {
    func draw() {
        print("Drawing circle")
    }
}
medium
A. Circle struct must use 'override' keyword for draw().
B. Protocol Drawable is missing required methods.
C. Class Shape cannot conform to protocol Drawable.
D. Struct cannot inherit from a class; Circle cannot inherit from Shape.

Solution

  1. 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.
  2. Step 2: Verify protocol conformance and method implementation

    Shape correctly conforms to Drawable. Circle tries to override draw(), but inheritance is invalid for structs.
  3. Final Answer:

    Struct cannot inherit from a class; Circle cannot inherit from Shape. -> Option D
  4. Quick Check:

    Structs cannot inherit classes in Swift [OK]
Hint: Structs cannot inherit classes; use protocols for shared behavior [OK]
Common Mistakes:
  • Thinking structs can inherit classes
  • Confusing protocol conformance with inheritance
  • Using 'override' in struct methods
5. You want to design a Swift app where multiple types share behavior but also have unique data. You want flexibility to add new behaviors without deep inheritance chains. Which approach fits best?
hard
A. Use deep class inheritance with base classes and subclasses for all types.
B. Use Protocol-Oriented Programming with protocols and structs to share behavior and data.
C. Use only classes without protocols to simplify code.
D. Use global functions instead of protocols or classes.

Solution

  1. Step 1: Analyze design goals

    The goal is to share behavior, have unique data, and avoid deep inheritance chains for flexibility.
  2. 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.
  3. Final Answer:

    Use Protocol-Oriented Programming with protocols and structs to share behavior and data. -> Option B
  4. Quick Check:

    Flexible behavior sharing = POP with protocols & structs [OK]
Hint: For flexible shared behavior, prefer POP with protocols and structs [OK]
Common Mistakes:
  • Choosing deep inheritance causing rigid design
  • Ignoring protocols for behavior sharing
  • Using global functions losing type safety
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard