29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

model<br />

4. update<br />

1. bal<strong>an</strong>ce<br />

(view)<br />

0<br />

Deposit<br />

(statement)<br />

bal<strong>an</strong>ce<br />

2. deposit<br />

(controller)<br />

3. ch<strong>an</strong>ged:<br />

#bal<strong>an</strong>ce<br />

Figure 26.8: Tracing the interactions in the MVC<br />

The view is displaying a very simple window, which c<strong>an</strong> display the curre nt bal<strong>an</strong>ce <strong><strong>an</strong>d</strong> a button<br />

labeled deposit. The controller is responsible for responding to the button click.<br />

The sequence of message which would be sent between these objects as the window was created <strong><strong>an</strong>d</strong><br />

a user interacted with the window are illustrated in Figure 26.8 <strong><strong>an</strong>d</strong> described below:<br />

1. The statement object is inst<strong>an</strong>tiated <strong><strong>an</strong>d</strong> the view is opened. The view object must first draw the<br />

window. To determine the current bal<strong>an</strong>ce it must send the message bal<strong>an</strong>ce to its model ( the<br />

statement). The returned value is then displayed in the output field.<br />

The user clicks the mouse in the area of the screen used to display the button. The controller<br />

sees this click <strong><strong>an</strong>d</strong> enters into a dialogue with the view to determine if the click was actually<br />

within the button or not 12 . If it was, then the controller must send a message to the model to tell it<br />

to deposit ten pounds (we are assuming every deposit equals ten pounds for simplicity). This is<br />

done by sending the message:<br />

model deposit: 10.<br />

2. The model (a statement) receives the message deposit <strong><strong>an</strong>d</strong> records this. It then updates the<br />

current bal<strong>an</strong>ce <strong><strong>an</strong>d</strong> sends itself a ch<strong>an</strong>ged message, e.g.:<br />

bal<strong>an</strong>ce: newAmount<br />

bal<strong>an</strong>ce := newAmount<br />

self ch<strong>an</strong>ged: #bal<strong>an</strong>ce.<br />

3. Because the model sent itself the cha nged message, <strong>an</strong> update message is sent to its dependents,<br />

one of which is the view.<br />

4. The view receives the update message. Its implementation of update re -runs the code used to<br />

create the bal<strong>an</strong>ce view in the first place. That is, it sends the message bal<strong>an</strong> ce to the model as in<br />

step 1.<br />

There are a number of points worth noting about this example. Firstly, the view does not record<br />

<strong>an</strong>ywhere what the actual bal<strong>an</strong>ce is. It merely obtains the value from the model <strong><strong>an</strong>d</strong> displays it on the<br />

screen. From that moment on it forgets it. Secondly, the controller must ask the view to determine if the<br />

cursor is within the region of the button. That is, the controller does not know where on the screen the<br />

button is displayed, nor does it attempt to obtain that information fro m the view. Thirdly, even though<br />

the controller has direct access to the view, it does not tell the view what it is asking the model to do.<br />

The view is unaware of <strong>an</strong>y ch<strong>an</strong>ge in the model until it is sent <strong>an</strong> update message. At this point it<br />

requests the cur rent bal<strong>an</strong>ce from the model. Note that this me<strong>an</strong>s that if the statement was sent a<br />

deposit message by some other object (rather th<strong>an</strong> by the controller) it would still result in the view<br />

being told to update itself. Finally, the model is unaware that the message:<br />

deposit: 10.<br />

came from the controller. It could have come from <strong>an</strong>y object <strong><strong>an</strong>d</strong> thus the model is completely<br />

insulated from the controller <strong><strong>an</strong>d</strong> the view.<br />

12<br />

We will come back to this issue of which controller actually catches the input <strong><strong>an</strong>d</strong> which view is questioned about the<br />

position of the cursor at a later d ate.<br />

219

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!