I am using the MVP design pattern and I have registered my presenter as a listener on the various buttons and other interactive elements on my view. And that works fine, I get notified whenever a user performs some action in the GUI.
However I don’t the procedure for notifying the presenter when the model changes. I have data coming in from a server that gets processed in, for example, a User model, and changes the value of my User object.
How do you notify the presenter that the model has changed in Java?
Do I let my model hold a reference to the presenter and explicitly call something like presenter.userObjectHasBeenUpdated() in my model after I have altered the User object? Or is there a way of placing a listener on the User object that will automatically call presenter.userObjectHasBeenUpdated() when the User object gets modified?
I would say that you create a dedicated interface like Observer and you let your presenters implement it. Then each presenter can register themselves on the model objects to be notified whenever an object changes.
and in your model:
Of course you can improve all this by adding Thread-safety, typing your notification etc…