Swift Academy · Lesson

Protocol-Oriented Design Principles

Favouring protocol composition over class inheritance for flexible design.

Lesson 4 of 412 steps

Protocol-Oriented Design Principles is a free Swift Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

Welcome

Protocol-Oriented Programming (POP) favours protocol composition over class inheritance to build flexible, testable, and reusable Swift code.

The Problem with Inheritance

Deep class hierarchies cause problems: • Subclasses inherit ALL superclass state and behaviour • Changing a superclass can break subclasses • Can't inherit from two classes Protocols solve all three issues.

Start with a Protocol

```swift protocol Greetable { var greeting: String { get } func greet() } extension Greetable { func greet() { print(greeting) } // default implementation } ``` Protocol extensions provide defaults that any conformer gets for free.

Composing Behaviours

```swift protocol Persistable { func save() } protocol Networked { func fetch() async throws -> Data } protocol Loggable { func log(_ msg: String) } struct DataService: Persistable, Networked, Loggable { ... } ``` Mix behaviours like LEGO blocks — no class hierarchy needed.

Protocol Extensions for Shared Logic

```swift extension Loggable { func log(_ msg: String) { print("[\(type(of: self))] \(msg)") } } // Every Loggable type gets this for free ```

Retroactive Modelling

Protocols can be applied to types you don't own: ```swift protocol JSONConvertible { func toJSON() -> String } extension Int: JSONConvertible { func toJSON() -> String { "\(self)" } } ``` Add conformances to standard library types without subclassing.

Value Types + Protocols

Structs conforming to protocols give you value semantics *and* polymorphism: ```swift protocol Shape { var area: Double { get } } struct Circle: Shape { var radius: Double; var area: Double { .pi * radius * radius } } struct Square: Shape { var side: Double; var area: Double { side * side } } let shapes: [any Shape] = [Circle(radius:3), Square(side:4)] shapes.map { $0.area } // [28.27, 16] ```

Dependency Injection via Protocol

```swift protocol DataStore { func save(_ data: Data) } struct FileStore: DataStore { func save(_ d: Data) { /* disk */ } } struct MockStore: DataStore { func save(_ d: Data) { /* test log */ } } class Service { let store: DataStore init(store: DataStore) { self.store = store } } ```

When to Use Classes

Favour classes when: • You need shared mutable state (reference semantics) • Integration with ObjC/UIKit APIs • Inheritance is the correct model (rare) Default to protocols + structs for everything else.

Protocol Naming Conventions

• Capability protocols: suffix `-able`, `-ible`, `-ing` (Equatable, Hashable, Coding) • Delegate protocols: suffix `Delegate` or `DataSource` • Abstract role: noun (`Store`, `Parser`, `Renderer`) Good names make protocol-oriented code self-documenting.

Quick Check

What do protocol extensions provide to conforming types?

Recap

Key takeaways: • Prefer protocols + structs over class hierarchies • Protocol extensions provide shared default implementations • Compose behaviours from small single-purpose protocols • Protocols enable dependency injection for testability • Reserve classes for reference semantics and ObjC interop Course complete! Next: Value Types vs Reference Types in Depth.
Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “Protocol-Oriented Design Principles” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Protocol-Oriented Design Principles”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “Protocol-Oriented Design Principles”?

Favouring protocol composition over class inheritance for flexible design. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “Protocol-Oriented Design Principles” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Protocol Composition with &
  2. Conditional Conformance
  3. Self Requirements and Equatable Design
  4. Protocol-Oriented Design Principles
← Back to Swift Academy