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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Figure 26.5: Part of the Controller class hierarchy<br />

It might seem underst<strong><strong>an</strong>d</strong>able that the m odel should be separated out from the user interface, but<br />

why is the input separated from the output? This is for two reasons:<br />

• Firstly, it allows the controllers <strong><strong>an</strong>d</strong> the views to inherit from different class hierarchies. This<br />

allows the developer to overc ome the limitation of single inherit<strong>an</strong>ce enforced by <strong>Smalltalk</strong> (but<br />

not all object oriented l<strong>an</strong>guages).<br />

• Secondly, it allows different combinations of views <strong><strong>an</strong>d</strong> controllers. This may give different looks<br />

(views) or feels (controllers).<br />

26.2.3 How the MVC works<br />

You may notice that in Figure 26.6 the arrows only point from the view <strong><strong>an</strong>d</strong> controller to the model.<br />

There are no arrows from the model back to the view or controller. This is because the model does not<br />

directly know <strong>an</strong>ythi ng about its view or controller. This is partly because it does not need to know<br />

<strong>an</strong>ything about them <strong><strong>an</strong>d</strong> also because it enforces this separation. A common mistake for those new to<br />

the MVC is to find that they have created inst<strong>an</strong>ce variables in their model object which allow them to<br />

link the model directly to the view <strong><strong>an</strong>d</strong>/or controller. This is extremely poor <strong>Smalltalk</strong> style!<br />

So how do the view <strong><strong>an</strong>d</strong> the controller find out about ch<strong>an</strong>ges in the model. Remember, the view<br />

bases what it displays on the data held within the information model. In terms of <strong>Smalltalk</strong> relationships<br />

this me<strong>an</strong>s that the view is a dependent of the model (see last chapter). The view, must therefore, record<br />

itself as a dependent of the model ( Figure 26.6 illustrates how interaction occurs between the model,<br />

view <strong><strong>an</strong>d</strong> controller). This me<strong>an</strong>s that it is the responsibility of the model to inform its dependents of<br />

<strong>an</strong>y ch<strong>an</strong>ges to its contents (i.e. its view). This is done by the model sendin g a message to itself stating<br />

that its contents has ch<strong>an</strong>ged in some way. The ch<strong>an</strong>ged method is actually defined in class <strong>Object</strong><br />

<strong><strong>an</strong>d</strong> is thus inherited by all classes in the system. This me<strong>an</strong>s that <strong>an</strong>y object in the system c<strong>an</strong> act as the<br />

model in <strong>an</strong> MVC tria d 11 . This method causes <strong>an</strong> update: message to be sent to each of the views<br />

referenced in the dependents collection.<br />

11<br />

This actually me<strong>an</strong>s that <strong>an</strong>y object, irrespective of whether it is used in the MVC, c<strong>an</strong> have dependents , <strong><strong>an</strong>d</strong> that those<br />

dependents c<strong>an</strong> be informed of ch<strong>an</strong>ges to t he object is the same way that views are informed of ch<strong>an</strong>ges in their associated<br />

information model.<br />

217

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

Saved successfully!

Ooh no, something went wrong!