Updated 1 February 2021
The design pattern used to manage the complexity of code. so adapter pattern in iOS helps us to make the modular code.
And a better design pattern makes the code reusable. design pattern defines the purpose of the segment of code.
Adapter Pattern in iOS is pretty common in Swift code. this helps us works together with different objects.
Adapter Pattern in iOS is also known as a wrapper in iOS swift. it provides the control to use to functions of other objects.
So the Adapter use to connect the two different things and it’s the same work in programming also.
The adapter allows objects to manage with objects they could not normally work with due to different interfaces
We are using uitableview delegate and data source as Adapter and used their functions/delegates.
We will use the class adapter with protocol or interface and inheritance.
We will create one demo project to learning this topic.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() //MARK:- Run without Adapter print("Run without Adapter") let firstCarObj = FirstCarDeatils() firstCarObj.carDetails(name: "Swift", wheel: 4) //MARK: Run with Adapter print("Run with Adapter") let secondCarObj = SecondCarDeatils() let adapterObj = CarDeatilsAdaptor(objOfSecondCar: secondCarObj) adapterObj.carDetails(name: "Honda", wheel: 4) } } protocol Car { func carDetails(name: String, wheel: Int) } class FirstCarDeatils: Car { func carDetails(name: String, wheel: Int) { print("Car name is - \(name) and car have \(wheel) wheels" ) } } class SecondCarDeatils { func carDetails(deatils: String) { print("Second car details: " + deatils) } } class CarDeatilsAdaptor: Car { var secondCarObject: SecondCarDeatils? init(objOfSecondCar: SecondCarDeatils) { secondCarObject = objOfSecondCar } func carDetails(name: String, wheel: Int) { secondCarObject?.carDetails(deatils: "Car name is - \(name) and car have \(wheel) wheels") } } |
We have created the Car protocol with the car details function.
Now we have created the two classes. then we have created the car details adapter for using the protocol with different type instance.
And in cardetailsAdapter have a constructor that works as dependency injection and interact with car details fucntions.
And thanks for reading this blog, You can get more info from here
For more blog please click here.
So pls follow the above step and And if you have any issue or suggestion you can leave your message in the comment section I will try to solve this.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.