Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
• Views control what is displayed on the screen.<br />
• Controllers h<strong><strong>an</strong>d</strong>le user interaction.<br />
• Models hold the domain data to be displayed by the view.<br />
Therefore it is in the view class that the developer must define a method which will specify what the<br />
custom view widget will display. This method is the displayOn: method. It takes one parameter<br />
which is a GraphicsContext. GraphicsContexts are objects which work with windows to display<br />
graphical objects (determining how the graphic object should be rendered).<br />
The displayOn: method is sent by the system to the view whenever a ch<strong>an</strong>ge has occurred in one<br />
of the objects on which it depends (such as the model) or when the screen needs to be updated (for<br />
example, after a window which has been partially covering the view is closed).<br />
For the view to determine what it should display it needs to communicate with the model which<br />
holds the data. It must therefore have a link to the model (whether it is a separate object or the inst<strong>an</strong>ce<br />
of application model which holds the parent window).<br />
Finally, the view must also work with a controller. This controller will determine what action the<br />
application should take whenever the user interacts with the view widget. Note that this controller does<br />
not attempt to control how the user interacts with the whole window, rather it is only concerned with the<br />
user’s interaction with the view. For example, what happens when the user clicks in the view etc.<br />
We shall now consider how the SmallDraw application is structured.<br />
29.3 The structure of the application<br />
Figure 29.4 illustrates the inherit<strong>an</strong>ce structure between the classes used in the SmallDraw application.<br />
This class hierarchy is typical of <strong>an</strong> application which incorporates a custom view. For example, we<br />
have <strong>an</strong> ApplicationModel hierarchy with the main SmallDraw application. We have a<br />
Controller subclass ( DrawingController), a Model subclass ( DrawingModel) <strong><strong>an</strong>d</strong> a<br />
View subclass ( DrawingView). In addition we have three types of object which c<strong>an</strong> be drawn in a<br />
SmallDraw drawing: BoxWidget, CircleWidget <strong><strong>an</strong>d</strong> PointWidget. The only difference<br />
between these three classes is what is drawn on to the graphics context representing the view. The<br />
DrawingWidget class from which they all inherit defines common attributes used by all objects<br />
displayed within a DrawingView (such as t heir origin, the extent of their boundary <strong><strong>an</strong>d</strong> the size of<br />
their border). Each of these classes will be considered in more detail in a later section in this chapter.<br />
<strong>Object</strong><br />
Model<br />
DrawingModel ApplicationModel<br />
Controller<br />
view<br />
model<br />
View<br />
model<br />
controller<br />
DrawingWidget<br />
origin<br />
extent<br />
border<br />
style<br />
nodes<br />
SmallDraw<br />
drawingView<br />
ControllerWithMenu<br />
DrawingView<br />
defaultController<br />
BoxWidget<br />
CircleWidget<br />
PointWidget<br />
diameter<br />
drawingModel<br />
DrawingController<br />
Figure 29.4: Inherit<strong>an</strong>ce in SmallDraw<br />
Note that the dashed line between the <strong>Object</strong> class <strong><strong>an</strong>d</strong> the View class indicates that there are a<br />
number of classes between these two in the actual class hierarchy. It is useful to examine these classes<br />
yourself to see what facilities they add.<br />
246