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.

classVariableNames: ''<br />

poolDictionaries: ''<br />

category: 'SmallDraw'<br />

The initialize method for this class (defined in the<br />

values for each of the inst<strong>an</strong>ce variables. For example:<br />

initialize protocol) sets the default<br />

initialize<br />

origin := 0 @ 0.<br />

extent := 30 @ 30.<br />

border := Border width: 1.<br />

You c<strong>an</strong> experiment with these values by ch<strong>an</strong>ging the extent or the border (do so <strong><strong>an</strong>d</strong> see what the<br />

effects are).<br />

Next we will define the methods in the accessing protocol. These methods retrieve <strong><strong>an</strong>d</strong> set the<br />

border, the origin <strong><strong>an</strong>d</strong> the extent:<br />

border<br />

^border<br />

border: aBorder<br />

border := border.<br />

origin<br />

^origin<br />

origin: aPoint<br />

origin := aPoint.<br />

extent<br />

^extent<br />

extent: aPoint<br />

extent := aPoint<br />

The displaying protocol has two methods in it: displayOn: <strong><strong>an</strong>d</strong> bounds. The displayOn:<br />

method is a subclass responsibility. This is because each subclass needs to specify what it should look<br />

like independently. This is done in the displayOn: method.<br />

displayOn: aGraphicsContext<br />

self subclassResponsibility<br />

The bounds method calculates the bounds of the object using the origin <strong><strong>an</strong>d</strong> the extent. The<br />

return object is <strong>an</strong> inst<strong>an</strong>ce of rect<strong>an</strong>gle.<br />

bounds<br />

^origin extent: extent.<br />

Finally, as before we need to extend the basic inst<strong>an</strong>ce creation method new by se nding the message<br />

initialize to the newly created inst<strong>an</strong>ce. This is left as <strong>an</strong> exercise for the reader.<br />

29.5.6 The PointWidget class<br />

The PointWidget is a subclass of DrawingWidget that specifies how a point should be drawn in<br />

a drawing. It defines a single inst<strong>an</strong>ce variable diameter. This indicates the size of the point to draw:<br />

DrawingWidget subclass: #PointWidget<br />

inst<strong>an</strong>ceVariableNames: 'diameter '<br />

classVariableNames: ''<br />

poolDictionaries: ''<br />

category: 'SmallDraw'<br />

The initialize method specifies the default size of the diameter. As the DrawingWidget<br />

defines some initializations as well, we must send the message super initialize.<br />

initialize<br />

super initialize.<br />

diameter := 4.<br />

254

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

Saved successfully!

Ooh no, something went wrong!