Swift
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Optionals
-
Section 4: Object-Oriented and Value Types
-
Section 5: Memory Management
-
Section 6: Generics and Error Handling
-
Section 7: Concurrency
-
Section 8: Working with Collections
-
Section 9: Codable and Data Handling
-
Section 10: Protocol-Oriented Programming
-
Section 11: Testing and Tooling
-
Section 12: Practical Projects
-
Section 13: Interview Practice
-
Section 14: More Practice Exercises
-
Section 15: More Standard Library
-
Section 16: Advanced Concurrency
-
Section 17: Foundation Framework Deep Dive
-
Section 18: URLSession and Networking Deep Dive
-
Section 19: Combine Framework
-
Section 20: SwiftUI Fundamentals for Swift Developers
-
Section 21: Server-Side Swift with Vapor
-
Section 22: Swift Package Manager Deep Dive
-
Section 23: Swift Concurrency Deep Dive
-
Section 24: More Language Features
-
Section 25: Error Handling Deep Dive
-
Section 26: Testing Deep Dive
-
Section 27: Data Structures and Algorithms in Swift
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Swift Macros (Swift 5.9+)
-
Section 31: Property Wrappers Ecosystem
-
Section 32: Swift Interop Deep Dive
-
Section 33: iOS App Architecture Patterns
-
Section 34: Performance and Debugging
-
Section 35: App Distribution and CI/CD
-
Section 36: More Practical Projects
-
Section 37: SwiftData and Persistence
-
Section 38: More Design Patterns
-
Section 39: More Review and Practice
-
Section 40: More Foundation Deep Dive
-
Section 41: Advanced Collections in Swift
-
Section 42: Advanced Generics Practice
-
Section 43: UIKit for Legacy and Hybrid Apps
-
Section 44: watchOS and visionOS Development Basics
-
Section 45: More Networking Patterns
-
Section 46: More Testing Practice
-
Section 47: Accessibility in Swift Apps
-
Section 48: Localization
-
Section 49: More Practical Projects Round 2
-
Section 50: Swift Charts Framework
-
Section 51: More Interview and Algorithm Practice
-
Section 52: Final Practice and Mastery
-
Section 53: Swift Compiler and Build System
-
Section 54: More Concurrency Practice
-
Section 55: App Store Guidelines and Review
-
Section 56: More Design and Architecture
43: Protocol-Oriented Design Philosophy
Imagine you're hiring for a startup. In a traditional, old-school company, you might hire someone because they have a "Management Degree." That's their identity. Because they are a "Manager," they automatically inherit all the traits, burdens, and expectations of every other manager in the building, whether those traits are actually useful for the specific project at hand or not. This is how Class-based inheritance works; you define what an object is.
Now, imagine a more modern approach. Instead of looking for a "Manager," you look for a list of capabilities. You need someone who can "Write Python," "Manage a Budget," and "Speak Japanese." You don't care if they were a project lead, a freelance consultant, or a former accountant. As long as they can fulfill those specific roles, they're in. This is the heart of Protocol-Oriented Programming (POP). We stop asking "What are you?" and start asking "What can you do?"
Stop Thinking in Family Trees
In a lot of the languages you might have used before Swift, the instinct is to build a deep hierarchy. You'd make a Vehicle class, then a MotorizedVehicle subclass, then a Car subclass. It feels organized at first, but it quickly becomes a nightmare. What happens when you need a Boat that is also Motorized, but also needs to share logic with a FloatingObject? You can't inherit from two classes in Swift. You hit a wall.
When I shifted my mindset to POP, I stopped drawing family trees and started drawing checklists. Instead of a Vehicle base class, I create protocols like Drivable, Floatable, and Fuelable. A Car simply checks the Drivable and Fuelable boxes. A JetSki checks Drivable, Floatable, and Fuelable. It's infinitely more flexible because you're composing behavior rather than inheriting it.
Building Capabilities, Not Hierarchies
Let's look at how this actually looks in code. I'll avoid the typical "Animal" example because it's overused. Let's think about a game system where different entities can be damaged or can move.
protocol Damageable {
var health: Int { get set }
func takeDamage(_ amount: Int)
}
protocol Movable {
var position: CGPoint { get set }
func move(to point: CGPoint)
}
// A Player is both movable and damageable
struct Player: Damageable, Movable {
var health = 100
var position = CGPoint.zero
mutating func takeDamage(_ amount: Int) {
health -= amount
}
mutating func move(to point: CGPoint) {
position = point
}
}
// A Wall is only damageable
struct Wall: Damageable {
var health = 500
mutating func takeDamage(_ amount: Int) {
health -= amount
}
}
Notice how Wall and Player share no common parent class, yet I can write a function that handles anything Damageable. I don't have to care if it's a player, a wall, or a breakable crate. I just care that it has a takeDamage method.
The Magic of Protocol Extensions
Now, you might be thinking, "Wait, if I have 10 different Damageable objects, do I have to write the takeDamage logic 10 times?" This is where most people get stuck and try to go back to classes. But in Swift, we have Protocol Extensions.
Protocol extensions allow us to provide a default implementation. It's like providing a standard operating procedure for anyone who signs up for that "job description."
extension Damageable {
func printHealthStatus() {
print("Current health is \(health).")
}
// We can even provide a default way to take damage
mutating func takeDamage(_ amount: Int) {
health -= amount
print("Took \(amount) damage!")
}
}
By adding this extension, the Wall and Player structs from earlier no longer need to implement takeDamage themselves unless they want to do something special (like adding armor calculations for the player). They get the default behavior for free. I've effectively created a "mixin" of functionality without the rigid baggage of a base class.
When to actually use this
I'll be honest with you: you don't need to make everything a protocol. Over-engineering is a real risk here. Use POP when you find yourself wanting to share behavior across types that aren't logically part of the same "family." If you find yourself creating a base class just to avoid repeating a few methods, stop. That's a huge red flag. Create a protocol, add an extension, and keep your types lean and independent.
📋 Practical Task
Building a Modular Smart Home System
You are designing a system for a smart home. Instead of creating a complex class hierarchy (e.g., ElectronicDevice -> Light), you will use a Protocol-Oriented approach to handle different capabilities.
Requirements:
- Create a protocol
Powerablewith a boolean propertyisOnand a methodtogglePower(). Provide a default implementation fortogglePower()in a protocol extension. - Create a protocol
Dimmablewith a propertybrightness(Int, 0-100) and a methodsetBrightness(to: Int). - Create a protocol
Connectablewith a propertyipAddressand a methodconnectToWifi(). Provide a default implementation forconnectToWifi()that prints "Connecting to network...". - Implement three structs:
SmartBulb: Should bePowerable,Dimmable, andConnectable.SmartPlug: Should bePowerableandConnectable.BasicLamp: Should only bePowerable.
Testing your logic: Create an array of Powerable objects containing one of each device. Loop through them and call togglePower() on each to prove that the protocol-oriented design allows you to treat different types uniformly based on their capabilities.
There are no comments for now.