简体   繁体   中英

Communication between a widget, a service, and the model (singleton) of an application (MVC like architecture)

I got a complex question involving many different components of an application. I hope to be clear enough.

In my app, the application object, a singleton, provides a model (singleton too) to many activities. The main activity, presents a list of timed data to the user. These data come from the model.

I also have a widget and I want it to: - update when timed data is updated from within the model (via the GUI) - as time passes, update to only display the 2 first timed data of the list, removing the ones that are now obsolete.

To reach the second objective, I designed a service that asks the model to get some data, and uses timers to wakeup after the first data expired, and remove it from the list then sends a broadcast to the widget with some data in intent, that the widget will display.

This works fine.

But I also want the widget to update as soon as data changes in the model (not because time passes, but because of the user asking for some other data through the GUI).

But, and here is my problem:

how can the model tell the widget to update? Everything is in place for the service to update the widget but the engine can not know if there is a widget. I don't want the model to call the service as maybe there is no widget and no need to have an additional process for the service.

How can this interaction be redesigned elegantly?

I am really missing something here.

You can use the Observer pattern to have the engine fire off events to whichever components in the app are interested whenever things are updated, without the event-producing component having any real knowledge of the event-consumers.

The Event object sent as a parameter should contain all of the relevant info about the event for the UI widget to update itself, etc., so that only this abstract "event" object needs to be passed - and your layers do not bleed into each other.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM