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.

style := aSymbol<br />

Next we define <strong>an</strong> updating protocol. This protocol will hold a single method addWidgetAt: .<br />

This method adds the appropriate type of graphic object to the objects held in nodes. The inst<strong>an</strong>ce<br />

variable style is used to determine the type of object to add. The parameter aPoint indicates the<br />

location, within the drawing view, at which the object should be displayed. Finally, the method informs<br />

the drawingModel that something has ch<strong>an</strong>ged <strong><strong>an</strong>d</strong> that its dependents may be interested in that<br />

ch<strong>an</strong>ge.<br />

addWidgetAt: aPoint<br />

| aNode pos |<br />

self style = #point ifTrue: [aNode := PointWidget new].<br />

self style = #box ifTrue: [aNode := BoxWidget new].<br />

self style = #circle ifTrue: [aNode := CircleWidget new].<br />

aPoint = nil ifTrue: [pos := 0 @ 0] ifFalse: [pos := aPoint].<br />

self nodes add: (aNode origin: pos).<br />

self ch<strong>an</strong>ged: #object with: aNode.<br />

We will also define a single class side method new. This method will extend the functionality of the<br />

new method by sending the message initialize to a newly created inst<strong>an</strong>ce. The method is defined<br />

in the class side protocol inst<strong>an</strong>ce creation:<br />

new<br />

^super new initialize.<br />

29.5.3 The DrawingView class<br />

The DrawingView class is a subclass of the View class. It possesses a single inst<strong>an</strong>ce variable called<br />

defaultControllerClass. The contents of this variable will be used to inst<strong>an</strong>tiate the correct<br />

type of controller (in this case <strong>an</strong> inst<strong>an</strong>ce of DrawingController).<br />

View subclass: #DrawingView<br />

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

classVariableNames: ''<br />

poolDictionaries: ''<br />

category: 'SmallDraw'<br />

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

The initialize method for the DrawingView class is defined as :<br />

initialize<br />

super initialize.<br />

defaultControllerClass := DrawingController.<br />

This method is defined within the initialize protocol. To ensure that all initialization done in<br />

superclasses is also carried out in this class, the method first sends the message super initialize.<br />

It then initializes the defaultControllerClass inst<strong>an</strong>ce variable.<br />

Following the conventions laid down in the system view class hierarchy we will define a<br />

defaultControllerClass accessor method in a protocol referred to as controller accessing. This<br />

method returns the contents of the defaultControllerClass inst<strong>an</strong>ce variable:<br />

defaultControllerClass<br />

^defaultControllerClass<br />

In the model accessing protocol we will define <strong>an</strong> updater method for the model associated with the<br />

drawingView inst<strong>an</strong>ce called model:<br />

model: aModel<br />

super model: aModel.<br />

self invalidate.<br />

This method uses <strong>an</strong> inherited version of the model: method to set the model <strong><strong>an</strong>d</strong> then sends the<br />

message invalidate to itself. This informs the view that some part of what is currently being<br />

displayed may need to be redrawn. This will cause a displayOn: aGraphicsContext message<br />

to be sent to self.<br />

252

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

Saved successfully!

Ooh no, something went wrong!