0Pricing
Swift Academy · Lesson

Protocol-Oriented Design Principles

Favouring protocol composition over class inheritance for flexible design.

Protocol-Oriented Design Principles is a free Swift Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

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

Yes — the full text of “Protocol-Oriented Design Principles” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

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