Factories are used to create objects. I was wondering if it's against the purpose of a factory, if I would bind couple of objects together in the factory. Let me illustrate this with an example.
Let's say I have a factory that produces modal dialogs. The factory can instantiate a view, which will display on screen. Also I need some buttons to be present on the view and which should provide some functionality, let's say call a service via some delegation.
Depending on type of the modal dialog I want to be able to provide different buttons and different behaviours.
class ModalFactory {
static func createWeatherModal() -> ModalWindowProtocol {
let modal = SomeModal()
let closeButton = CloseButton()
let verifyWeatherButton = WeatherButton()
modal.addButton(verifyWeatherButton)
modal.addButton(closeButton)
let service = RemoteApi()
verifyWeatherButton.performAction {
service.calculateWeatherConditions()
}
closeButton.performAction {
modalal.dismiss()
}
}
static func createAdvancedWeatherModal() -> ModalWindowProtocol {
let modal = AdvancedWeatherModal()
let service = AdvancedWeatherService()
let weatherButton = WeatherButton()
let closeButton = CloseButton()
modal.addButton(weatherButton)
modal.addButton(closeButton)
weatherButton.performAction {
service.getAdvancedWeatherInfo()
}
closeButton.performAction {
modal.dismiss()
}
}
}
So, there you have it. The binding of an tap event happens in factory. Is this a good practice? And if not, then what would you suggest for this kind of dynamic modal creation and binding?
Thank you
Aucun commentaire:
Enregistrer un commentaire