Events - the other way round

Imagine you have an object that serves as a data base. In addition you have 3 display objects (e.g. 3 different list boxes) that show parts of the content of the data base.

Now: Every time the data base changes its content a “change event” should be raised by the data base object itself. The 3 list box objects should listen to the occurrence of this event and react to this event in their own way.

In my thinking this would be a clean OOP design, because the data base object would only send an event to any listening object regardless of who or how many objects are listening (encapsulation). So: Should I ever add a fourth list box object I would not have to add a single line of code to the data base object.

Is this possible or is my way of thinking false?

What you describe is entirely possible with an observer pattern, or notification pattern. I have a module in iOSKit that allows you to do exactly that. It’s the Notification_Center module and it is will work on Desktop and iOS (and probably web, though I’ve never tried it.)

I think it is used in some example code in iOSKit, otherwise the inline comments should explain how to use it, if not feel free to ask me.

Ok, thanks, but iOSKit is an extension. I wanted to know if XOJO is able to to this?

And I would be pleased if someone could give his/her comment to the following consideration:

In my thinking this would be a clean OOP design, because the data base object would only send an event to any listening object regardless of who or how many objects are listening (encapsulation). So: Should I ever add a fourth list box object I would not have to add a single line of code to the data base object.

It’s not natively built into Xojo, but with very little code you can easily put it together. Look into the two patterns I mentioned, observer and notification, if you want to do it yourself, or you can use some premade classes. I encourage you to check out the classes I mentioned, with them the only code you would have to add is to register the fourth list box to receive the notification, the database would remain unchanged as you desire.

Ok, I’ll try.