Navigation:  Programming Cookbook > Creating GUI Applications > Pluggability >

Sharing a Model

Previous pageReturn to chapter overviewNext page

Since we are using an Observer pattern we get the benefit of allowing components to share the model's data.

Normally, since a presenter creates and associates itself with a default model, you might consider that it effectively owns this model data. For most purposes this makes sense but you might want to use two presenters to display and edit the same data. Naturally, you'd want both to be kept up to date with changes made by the other. To achieve this, both presenters may clip onto a separately created model or one presenter may share its model with another.

In this situation, each view is kept up-to-date with changes made to the model either by the other view (or possibly by some other component). As the model is changed it triggers change notifications that both views can intercept to cause themselves to refresh their displays. To demonstrate this, try the following example.

m := false asValue.

BooleanPresenter showOn: m.

BooleanPresenter show: 'Push to toggle' on: m.

BooleanPresenter show 'Yes-no text' on: m.

Here, the model is a ValueModel wrapping an instance (initially) of False. Onto this we connect three BooleanPresenters each with different views for displaying the model's value. As you click in each of the buttons, the model is changed and the other views update to reflect the new value.