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.

The next method we shall consider is the setBoxStyle method. This method is executed when<br />

the Box button is selected. This method sends the style: message to the drawingModel with the<br />

parameter #box. That is, it causes the current graphic object style to be set to #box.<br />

setBoxStyle<br />

drawingModel style: #box.<br />

In addition there are two other methods which set the style of object to be added. These are<br />

setCircleStyle <strong><strong>an</strong>d</strong> setPointStyle. They are exactly t he same as the setBoxStyle<br />

method except that they pass the symbols #circle <strong><strong>an</strong>d</strong> #point to the drawingModel via the<br />

message style: . The setCircleStyle method is called whenever the Circle button is selected<br />

<strong><strong>an</strong>d</strong> the setPointStyle method whenever the Point button is selected:<br />

setCircleStyle<br />

drawingModel style: #circle.<br />

setPointStyle<br />

drawingModel style: #point.<br />

Finally the aspects protocol (automatically generated for you if you have used the definer) will<br />

contain a single method called drawingView. This is <strong>an</strong> accessor method for the aspect<br />

drawingView:<br />

drawingView<br />

^drawingView<br />

As usual the SmallDraw application is inst<strong>an</strong>tiated by sending the message<br />

SmallDraw. For example:<br />

open to the class<br />

29.5.2 The DrawingModel class<br />

SmallDraw open<br />

The DrawingModel class is a subclass of Model. It possesses two inst<strong>an</strong>ce variables nodes <strong><strong>an</strong>d</strong><br />

style. The inst<strong>an</strong>ce variable style indicates the type of object to add to the collection of objects in<br />

nodes. The class definition is:<br />

Model subclass: #DrawingModel<br />

inst<strong>an</strong>ceVariableNames: 'nodes style '<br />

classVariableNames: ''<br />

poolDictionaries: ''<br />

category: 'SmallDraw'<br />

The DrawingModel class comment is left as <strong>an</strong> exercise for the reader.<br />

Having defined the DrawingModel class <strong><strong>an</strong>d</strong> class comment we are now ready to define the<br />

initialize method. As usual we will place this method in <strong>an</strong> initialize method protocol. The<br />

initialize method initializes the two inst<strong>an</strong>ce variables nodes <strong><strong>an</strong>d</strong> style. A new ordered<br />

collection is inst<strong>an</strong>tiated for the first <strong><strong>an</strong>d</strong> the style: accessor method is used to set the second (with a<br />

default style of #box):<br />

initialize<br />

nodes := OrderedCollection new.<br />

self style: #box.<br />

We are now ready to define the accessing methods for DrawingModel. These methods (defined<br />

within <strong>an</strong> accessing protocol) access the inst<strong>an</strong>ce variabl es of the class. For example, nodes <strong><strong>an</strong>d</strong><br />

style retrieve the value of their associated inst<strong>an</strong>ce variables, while style: resets the inst<strong>an</strong>ce<br />

variable style.<br />

nodes<br />

^nodes.<br />

style<br />

^style<br />

style: aSymbol<br />

251

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

Saved successfully!

Ooh no, something went wrong!