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.

Figure 12.2: The Org<strong>an</strong>izer class definition<br />

The class comment has been defined as follows. Remember providing a class comment is a good<br />

habit to get into.<br />

Class: Org<strong>an</strong>izer Author: John Hunt Date: 27 September 1995<br />

Purpose: This is a class definition which provides some of the facilities of a<br />

personal Org<strong>an</strong>izer.<br />

History:<br />

27 September 1995 John Hunt New Class<br />

Inst<strong>an</strong>ce variables:<br />

addressBook Holds names <strong><strong>an</strong>d</strong> addresses.<br />

Appointments Holds dates <strong><strong>an</strong>d</strong> things to do.<br />

notes Scratch pad for notes<br />

Usage:<br />

To try the class out select the following <strong><strong>an</strong>d</strong> 'do it'.<br />

|temp |<br />

temp := Org<strong>an</strong>izer new.<br />

temp newAddress: 'Room 47' for: 'John'.<br />

temp newAddress: 'Room 46' for: 'Patrick'.<br />

temp newAppointment: 'Meet with MEng' for: '10/10/95'.<br />

temp addNote: 'I must do all my work'.<br />

temp addNote: 'Today is a br<strong><strong>an</strong>d</strong> new day'.<br />

temp addressFor: 'John'.<br />

temp appointmentFor: '10/10/95'.<br />

temp printNotes.<br />

temp inspect.<br />

12.4 The initialize-release protocol<br />

The first protocol we will define will be the “initialize -release” protocol. As usual we will define the<br />

initialize method. This method will be us ed to initialize each of the inst<strong>an</strong>ce variables with appropriate<br />

objects. This is necessary for the other methods to function correctly. The addressBook is initialized<br />

to hold a Dictionary object as is the appointments inst<strong>an</strong>ce variable, while the notes inst<strong>an</strong>ce<br />

variable holds a Bag. Note however, that we do not access the inst<strong>an</strong>ce variables directly, even though<br />

we c<strong>an</strong> as we are within the same class. This is because, by using the updater methods we c<strong>an</strong> ensure<br />

that <strong>an</strong>y modification relating to how the Orga nizer class represents <strong>an</strong> addressBook, appointments or<br />

notes will be localized to the methods which access <strong><strong>an</strong>d</strong> update the inst<strong>an</strong>ce variables. Otherwise, if we<br />

ch<strong>an</strong>ged the internal representation we would have to modify every method which accessed them. Thi s<br />

could make mainten<strong>an</strong>ce very difficult.<br />

Figure 12.3 illustrates the System Browser with the initialize method for the Org<strong>an</strong>izer class. Your<br />

definition should look like this.<br />

105

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

Saved successfully!

Ooh no, something went wrong!