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.

29.4.1 Initialization<br />

3. model:<br />

initialize<br />

(SmallDraw)<br />

1. new<br />

DrawingView<br />

(DrawingView)<br />

4. addDependent:<br />

2. new<br />

DrawingModel<br />

(DrawingModel)<br />

Figure 29.6: <strong>Object</strong> interaction during initialization<br />

When the SmallDraw application is opened (i.e. when the message open is se nt to the SmallDraw<br />

class) <strong>an</strong> inst<strong>an</strong>ce of SmallDraw is created <strong><strong>an</strong>d</strong> sent the message initialize (as illustrated in<br />

Figure 29.6). This results in the creation of two further inst<strong>an</strong>ces one a drawingView <strong><strong>an</strong>d</strong> the other a<br />

drawingModel. The drawingController is automatically inst<strong>an</strong>tiated when the view is<br />

displayed (note the type of controller to be used is specified by the drawingView in the inst<strong>an</strong>ce<br />

variable defaultController).<br />

Once the two inst<strong>an</strong>ces have been created the mes sage model: is sent to the drawingView. This<br />

in turn causes the drawingView to become one of the drawing models dependents. Note that this last<br />

message send is defined in the DependentPart class, which is one of the superclasses of<br />

DrawingView.<br />

At this poi nt the structure required by the custom view is in place. It is now possible for the<br />

application model SmallDraw <strong><strong>an</strong>d</strong> the user to interact with the drawingView <strong><strong>an</strong>d</strong><br />

drawingController.<br />

29.4.2 Ch<strong>an</strong>ging the type of graphic object to be displayed<br />

setBoxStyle<br />

(SmallDraw)<br />

1. style:<br />

#box<br />

(DrawingModel)<br />

Figure 29.7: Ch<strong>an</strong>ging the graphic object to be added<br />

As we have used the user interface builder to add buttons to the main window which will be used to<br />

determine which type of graphic object to add to the d rawing, we require some way of passing that<br />

information onto the objects involved in m<strong>an</strong>aging <strong><strong>an</strong>d</strong> maintaining the custom view. That me<strong>an</strong>s we<br />

must inform the model of the type of object which will be added.<br />

The actual interaction illustrated in Figure 29.7 involves the message setBoxStyle being sent to<br />

the SmallDraw application when the Box style button is selected in the main window. This message is<br />

sent in response to <strong>an</strong> event being raised (we do not cover how this work s in this book: it is left as <strong>an</strong><br />

exercise for the reader).<br />

When the message setBoxStyle is received by the smallDraw object, a further message is<br />

then sent to the drawingModel (which smallDraw has a link to via the inst<strong>an</strong>ce variable<br />

drawingModel) setting the current style to the symbol #box. The style value c<strong>an</strong> currently be one of<br />

#box, #circle or #point.<br />

29.4.3 Adding a new graphic object<br />

(OrderedCollection)<br />

4. add: (BoxWidget)<br />

redButton<br />

Activity<br />

(DrawingController)<br />

1. addWidgetAt: 2. new<br />

(DrawingModel)<br />

BoxWidget<br />

3. origin:<br />

8. displayOn:forDisplayBox:<br />

5. ch<strong>an</strong>ged:with:<br />

(BoxWidget)<br />

(border)<br />

6. update:with:<br />

(DrawingView)<br />

7. displayOn: (GraphicsContext)<br />

9. displayRect<strong>an</strong>gle:<br />

(GraphicsContext)<br />

Figure 29.8: Adding a box graphic object<br />

248

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

Saved successfully!

Ooh no, something went wrong!