Skip to main content

Factories and event binding [Resolved]

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


Question Credit: DS888
Question Reference
Asked August 28, 2018
Posted Under: Programming
13 views
2 Answers

While the caller requires no direct input on how to create the object being produced by the Factory, a Factory pattern is perfectly fine, even when you're producing multiple objects.

If you want to be more dynamic and allow flexibility in creation, without giving up the advantage of leaving it to the class to deal with the specific implementations, consider using a Builder pattern.

The example they use is a CarBuilder which lets you build a Car by specifying various aspects of that car. If you think you can make both a "weather modal" and an "advanced weather modal" using this class, then great! Builder pattern is for you! If not, don't force the class to handle both. Create a second builder as necessary in that case.


credit: Neil
Answered August 28, 2018

If it's good enough for Bill Gates...

https://docs.microsoft.com/en-us/dotnet/framework/wpf/app-development/dialog-boxes-overview

However, it's not very MVVM. You don't say what language or framework you are using, but I think if you did all your controls like this you would end up with a lot of view logic in the factory class, which depending on your framework might not be best practice


credit: Ewan
Answered August 28, 2018
Your Answer
D:\Adnan\Candoerz\CandoProject\vQA