0
0
Swiftprogramming~10 mins

Why protocol-oriented design matters in Swift - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why protocol-oriented design matters
Define Protocol
Create Types conforming to Protocol
Use Protocol as Interface
Write Generic Code using Protocol
Easily Extend with New Types
Achieve Flexible, Reusable Code
Protocol-oriented design starts by defining protocols, then types conform to them, enabling flexible and reusable code through generic programming.
Execution Sample
Swift
protocol Greetable {
    func greet() -> String
}

struct Person: Greetable {
    func greet() -> String { "Hello!" }
}

let p = Person()
print(p.greet())
Defines a protocol and a struct that conforms to it, then calls the protocol method.
Execution Table
StepActionEvaluationResult
1Define protocol Greetable with greet()Protocol createdGreetable protocol ready
2Define struct Person conforming to GreetablePerson implements greet()Person type ready
3Create instance p of Personp is PersonInstance p created
4Call p.greet()Calls Person.greet()"Hello!" returned
5Print resultOutput to consoleHello!
💡 Program ends after printing greeting
Variable Tracker
VariableStartAfter Step 3After Step 4Final
pundefinedPerson instancePerson instancePerson instance
greet() outputundefinedundefined"Hello!""Hello!"
Key Moments - 3 Insights
Why do we use a protocol instead of just a class or struct?
Protocols let us define a common interface that many types can share, as shown in execution_table step 1 and 2, enabling flexible and reusable code.
How does calling greet() on p know which code to run?
Because p is a Person instance that conforms to Greetable, calling greet() runs Person's implementation (step 4 in execution_table).
What if we add another type conforming to Greetable?
We can add new types easily without changing existing code, making the design flexible (concept_flow last steps).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of p.greet() at step 4?
A"Hi!"
B"Hello!"
CError
DNo output
💡 Hint
Check execution_table row 4 under Result column
At which step is the Person instance created?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at execution_table row 3 Action column
If we add a new struct conforming to Greetable, what changes in the execution_table?
AStep 4 output changes
BStep 1 protocol changes
CNew steps showing the new struct definition and usage
DNo changes at all
💡 Hint
Refer to key_moments about adding new types and concept_flow steps
Concept Snapshot
protocol ProtocolName {
    func method() -> ReturnType
}

struct TypeName: ProtocolName {
    func method() -> ReturnType { ... }
}

- Protocols define shared interfaces.
- Types conform to protocols to implement behavior.
- Enables flexible, reusable, and testable code.
Full Transcript
Protocol-oriented design in Swift means defining protocols as blueprints for behavior. Types like structs or classes then conform to these protocols by implementing required methods. This approach allows writing generic code that works with any type conforming to the protocol, making code flexible and reusable. In the example, a protocol Greetable defines a greet method. The struct Person conforms by implementing greet. Creating a Person instance and calling greet prints Hello!. This design lets you add new types conforming to Greetable without changing existing code, promoting easy extension and maintenance.