0
0
Swiftprogramming~30 mins

Protocol composition in practice in Swift - Mini Project: Build & Apply

Choose your learning style9 modes available
Protocol composition in practice
📖 Scenario: Imagine you are building a simple app that manages different types of vehicles. Each vehicle can have different capabilities like driving, flying, or floating on water. You want to organize these capabilities using protocols and combine them to create specific vehicle types.
🎯 Goal: You will create protocols for different vehicle capabilities, then use protocol composition to define specific vehicle types that combine these capabilities. Finally, you will create instances of these vehicles and demonstrate their abilities.
📋 What You'll Learn
Create protocols named Drivable, Flyable, and Floatable each with one method signature.
Create a struct named Car that conforms to Drivable.
Create a struct named AmphibiousCar that conforms to both Drivable and Floatable using protocol composition.
Create a struct named FlyingCar that conforms to Drivable and Flyable using protocol composition.
Write a function that accepts a parameter conforming to both Drivable and Flyable and calls their methods.
Print outputs demonstrating the capabilities of each vehicle.
💡 Why This Matters
🌍 Real World
Protocol composition is useful when you want to build flexible and reusable code that models real-world objects with multiple capabilities, like vehicles that can drive, fly, or float.
💼 Career
Understanding protocol composition is important for Swift developers working on apps that require clean architecture and modular design, such as games, simulations, or complex user interfaces.
Progress0 / 4 steps
1
Create protocols for vehicle capabilities
Create three protocols named Drivable, Flyable, and Floatable. Each protocol should have one method signature: drive() for Drivable, fly() for Flyable, and float() for Floatable.
Swift
Need a hint?

Protocols define methods without implementation. Use protocol keyword and declare one method per protocol.

2
Create a Car struct conforming to Drivable
Create a struct named Car that conforms to the Drivable protocol. Implement the drive() method to print "Car is driving".
Swift
Need a hint?

Use struct Car: Drivable and implement drive() with a print statement.

3
Create AmphibiousCar and FlyingCar with protocol composition
Create a struct named AmphibiousCar that conforms to both Drivable and Floatable. Implement drive() to print "AmphibiousCar is driving" and float() to print "AmphibiousCar is floating". Then create a struct named FlyingCar that conforms to Drivable and Flyable. Implement drive() to print "FlyingCar is driving" and fly() to print "FlyingCar is flying".
Swift
Need a hint?

Use commas to conform to multiple protocols. Implement all required methods with print statements.

4
Use protocol composition in a function and print outputs
Write a function named testDriveAndFly that accepts one parameter named vehicle of type Drivable & Flyable. Inside the function, call drive() and fly() on vehicle. Then create instances of Car, AmphibiousCar, and FlyingCar. Call drive() on Car and AmphibiousCar, call float() on AmphibiousCar, and call testDriveAndFly with the FlyingCar instance.
Swift
Need a hint?

Use Drivable & Flyable as the parameter type. Call methods on instances to see printed messages.