Protocol-oriented design helps you write clear and flexible code. It makes your programs easier to change and reuse.
Why protocol-oriented design matters in Swift
protocol SomeProtocol { func doSomething() } struct SomeStruct: SomeProtocol { func doSomething() { print("Doing something") } }
A protocol defines a set of methods or properties that a type must have.
Types like struct, class, or enum can adopt protocols to promise they implement those methods.
Drivable and a struct Car that follows it by implementing drive().protocol Drivable { func drive() } struct Car: Drivable { func drive() { print("Car is driving") } }
Flyable is a protocol for flying behavior, and Bird adopts it.protocol Flyable { func fly() } struct Bird: Flyable { func fly() { print("Bird is flying") } }
User must have an id string.protocol Identifiable { var id: String { get } } struct User: Identifiable { var id: String }
This program shows how two different types, Person and Robot, can both greet because they follow the Greetable protocol. The function sayHello works with any Greetable type.
protocol Greetable { func greet() } struct Person: Greetable { var name: String func greet() { print("Hello, my name is \(name)!") } } struct Robot: Greetable { func greet() { print("Beep boop. I am a robot.") } } func sayHello(to greeter: Greetable) { greeter.greet() } let alice = Person(name: "Alice") let bot = Robot() sayHello(to: alice) sayHello(to: bot)
Protocols let you write code that works with many types, as long as they follow the same rules.
Using protocols helps avoid repeating code and makes your app easier to update later.
Protocol-oriented design is a key idea in Swift to build flexible and clean programs.
Protocols define shared behavior that many types can use.
Protocol-oriented design helps make code reusable and easy to change.
It encourages thinking about what things do, not just what they are.
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
