Created
April 8, 2022 11:06
-
-
Save boudhayan/d81977490607de2acb1b6f24c3e43bfe to your computer and use it in GitHub Desktop.
Protocol and Delegate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| //MARK: Protocol | |
| /* | |
| protocol is a set of rules, whichever class adopts the protocol, needs to implement the variables and functions declared inside that protocol. | |
| For the below protocol Car, it has two requirements, model and topSpeed. | |
| So if any class or struct adopts this protocol, needs to implement model and topSpeed. | |
| */ | |
| protocol Car { | |
| var model: String { get } | |
| func topSpeed() -> Int | |
| } | |
| /* | |
| Audi class adopts the Car protocol, so it has to define the model variable and topSpeed method, otherwise you will get compilation error. | |
| */ | |
| class Audi: Car { | |
| var model: String { | |
| return "Audi Q7" | |
| } | |
| func topSpeed() -> Int { | |
| return 250 | |
| } | |
| } | |
| //MARK: Delegate | |
| /* | |
| delegate is just the one to one communication design pattern, means if there are some methods defined in class A, and you want to call those methods/variables from class B, then you can use delegate. | |
| But whatever the method you will define in class A, that you need to declare first inside one protocol and class A needs to adopt that protocol. | |
| delegate just uses protocol in its implementation. | |
| */ | |
| // Flyable protocol declares two requirement, canFly and topSpeed | |
| protocol Flyable { | |
| func canFly() -> Bool | |
| func topSpeed() -> Int | |
| } | |
| // Aeroplane class adopts the Flyable protocol and defines the two protocol methods | |
| class Aeroplane: Flyable { | |
| func canFly() -> Bool { | |
| return true | |
| } | |
| func topSpeed() -> Int { | |
| return 1200 | |
| } | |
| } | |
| //Transport class uses delegation pattern. It wants to call methods of Aeroplane class. For that it holds one reference named delegate of type Flybale protocol | |
| class Transport { | |
| var delegate: Flyable? | |
| func speedOfCurrentTransport() { | |
| if let delegate = delegate { | |
| // from here it sends message to Aeroplane class, asking the top speed of it | |
| print(delegate.topSpeed()) | |
| } | |
| } | |
| func modeOfTransport() { | |
| // from here it sends message to Aeroplane class, asking whether it can fly or not | |
| if let delegate = delegate, delegate.canFly() { | |
| print("Aeroplane") | |
| } | |
| } | |
| } | |
| let airbus = Aeroplane() | |
| let transport = Transport() | |
| transport.delegate = airbus | |
| // when you call speedOfCurrentTransport, it will internally call `topSpeed` method of Aeroplane class | |
| transport.speedOfCurrentTransport() | |
| // when you call modeOfTransport, it will internally call `topSpeed` method of Aeroplane class | |
| transport.modeOfTransport() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment