In this sample project there are 2 main classes Person and Car. Any peson can buy the Car of type Sedan or SUV from the Car Show Room and that person can drive the car which he has purchased. So as per this idea, the classes and protocol are designed in Swift programming language are:
Classes
- Person
- Sedan
- SUV
- CarShowRoom
protocol:
- Car
Car is designed as protocol, which will work as interface for the different types of Car as Sedan, SUV etc.
//Tom has purchased the Sedan type Car, so Tom drive a Sedan
let Tom = Person("Tom")
print("Person name:\(Tom.personName)")
Tom.buyCar(type: .sedan)
Tom.drive()
//David has purchased the SUV type Car, so David drive a SUV
let David = Person("David")
print("Person name:\(David.personName)")
David.drive()
//If any one buy Sedan Car from the Car Show Room , he can drive a Sedan
let sedan = CarShowRoom.buyCar(type: .sedan)
print("Car Model:\(sedan.carModel)")
sedan.drive()
//If any one buy SUV Car from the Car Show Room , he can drive a SUV
let suv = CarShowRoom.buyCar(type: .suv)
print("Car Model:\(suv.carModel)")
suv.drive()