How can controls be notified about changes in the data?

If you have several views/controls/windows showing information coming from the same data source (not a database, an instance of a class that contains the information shown). Imaging that the data changes, which are the mechanisms to communicate those changes to the views?
Just an example, if you open different workspaces in the Xojo IDE showing the same file, all are updated at the same time.

In other frameworks I know about binding (never used by myself, but I have seen in other threads that this mechanism has been deprecated in Xojo), or notifications.

This is the one I used in iOS, but there is a Notification Center that support it (I know there is a plugin but I don’t want a specific solution for mac)…
My implementation was, the views are subscribed to one or several types of notifications (let’s say: ONEDATA_CHANGED(data) or ONEDATA_WILL_BE_REMOVED(data)), so each time the view receives this notification it checks if it’s showing this data and reacts to this.

And in case you have to build your own solution, any guideline?


I just saw this thread about the topic:

basically pointing to the observer pattern in the examples.

A class interface is a good method to do this. If you have an interface called DataUpdateReceiver then all of the windows/container controls/etc which implement the interface can receive the data update. In the class which stores the data, you can have an array:

receivers() as DataUpdateReceiver

and a register method and remove method:

[code]sub RegisterReceiver(newReceiver as DataUpdateReceiver)
receivers.append newReceiver
end sub

sub RemoveReceiver(receiverToRemove as DataUpdateReceiver)
dim theIndex as integer = receivers.indexOf(receiverToRemove)
if theIndex>=0 then
receivers.remove theIndex
end if
end sub
Then when the data is updated in the data storage class you can have a function to notify all the receivers that an update occurred:

sub NotifyReceiversOfChange(Data as string) for each DUR as DataNotificationReceiver in receivers DUR.DataUpdated(Data) next end sub
where DataUpdated(newData as String) is a method in the DataUpdateReceiver interface.

Each window/container control/etc will then have to implement the interface and its methods and register with the data storage class to receive updates.