Junior — Middle
What design patterns do you apply in your practice?
sobes.tech AI
Answer from AI
In iOS development practice, the following design patterns are often used:
- MVC (Model-View-Controller) — separation of data, interface, and control logic.
- MVVM (Model-View-ViewModel) — separation of presentation from business logic using ViewModel, often with data binding.
- Singleton — for creating a single instance of a class, such as a settings manager or session.
- Delegate — for passing events between objects, widely used in UIKit.
- Observer (NotificationCenter, KVO) — for responding to state changes.
- Factory — for creating objects without specifying exact classes.
Example of using Delegate:
protocol DataReceiverDelegate: AnyObject {
func didReceiveData(_ data: String)
}
class DataProvider {
weak var delegate: DataReceiverDelegate?
func fetchData() {
// Fetch data
let data = "Sample data"
delegate?.didReceiveData(data)
}
}
class ViewController: UIViewController, DataReceiverDelegate {
let provider = DataProvider()
override func viewDidLoad() {
super.viewDidLoad()
provider.delegate = self
provider.fetchData()
}
func didReceiveData(_ data: String) {
print("Data received: \(data)")
}
}