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.

29.5 The classes involved<br />

This section describes how to implement the classes in the SmallDraw application in detail (see Figure<br />

29.4 which illustrates the classes in this application). We will first consider the SmallDraw application<br />

itself before considering the three classes involved in the MVC framework ( DrawingModel,<br />

DrawingView <strong><strong>an</strong>d</strong> DrawingController) <strong><strong>an</strong>d</strong> wi ll conclude by describing the “widget” classes:<br />

DrawingWidget, PointWidget, CircleWidget <strong><strong>an</strong>d</strong> PointWidget.<br />

29.5.1 The SmallDraw class<br />

The SmallDraw class is a subclass of ApplicationModel. It possesses two inst<strong>an</strong>ce variables,<br />

drawingView (which is actually <strong>an</strong> aspe ct referencing the custom view) <strong><strong>an</strong>d</strong> drawingModel. The<br />

class definition is presented below <strong><strong>an</strong>d</strong> c<strong>an</strong> be typed in using the System Browser. Note that this<br />

assumes that the class is being defined in a class category called SmallDraw.<br />

ApplicationModel subclass: #SmallDraw<br />

inst<strong>an</strong>ceVariableNames: 'drawingView drawingModel '<br />

classVariableNames: ''<br />

poolDictionaries: ''<br />

category: 'SmallDraw'!<br />

The SmallDraw application provides the main window for the application. It possesses four buttons, <strong><strong>an</strong>d</strong><br />

a view as illustrated in Figure 29.10. You should use the c<strong>an</strong>vas tool <strong><strong>an</strong>d</strong> the palette to create a window<br />

which resembles this. Next use the properties tool to define the properties for the four buttons <strong><strong>an</strong>d</strong> the<br />

view. The figure illustrates the actions defined for each button <strong><strong>an</strong>d</strong> the aspect name of the custom view.<br />

Action: setPointStyle<br />

Action: setBoxStyle<br />

Action: setCircleStyle<br />

Action: exit<br />

View: drawingView<br />

Figure 29.10: Defining the graphic elements of the SmallDraw application<br />

Next use the definer tool from t he c<strong>an</strong>vas tools Launcher to create stub methods for the actions<br />

illustrated in Figure 29.10.<br />

We shall now define the initialize method in the initialize protocol in the SmallDraw class<br />

(use a browser to do this). The initialize method creates <strong>an</strong> inst<strong>an</strong>ce of the DrawingView class,<br />

the DrawingModel class <strong><strong>an</strong>d</strong> links them together. The method is presented below:<br />

initialize<br />

drawingView := DrawingView new.<br />

drawingModel := DrawingModel new.<br />

drawingView model: drawingModel.<br />

You are now ready to define each of the action methods in the actions protocol. The first method we<br />

shall consider is the exit method which is executed when the exit button is selected. This message<br />

uses the closeRequest method we saw in the last chapter to terminate the application:<br />

exit<br />

self closeRequest<br />

250

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

Saved successfully!

Ooh no, something went wrong!