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.

Wash wipe<br />

switch<br />

Wiper motor<br />

Relay<br />

Fuse<br />

Pump<br />

Water bottle<br />

Figure 3.2: <strong>Object</strong>s in simulation system 1<br />

Notice that I have named the objects after the element associated with the data item (e.g. the element<br />

associated with the fuse condition is naturally the fuse itself) <strong><strong>an</strong>d</strong> that the actual data items i nvolved<br />

(e.g. the condition of the fuse or the level of the water in the bottle) is <strong>an</strong> inst<strong>an</strong>ce variable of the object.<br />

This is a very common way of naming objects <strong><strong>an</strong>d</strong> their inst<strong>an</strong>ce variables. We now have the basic<br />

objects required for our application.<br />

3.5 Identifying the services/methods<br />

At the moment all we have are a set of objects each of which c<strong>an</strong> hold some data. For example, the<br />

water bottle c<strong>an</strong> hold <strong>an</strong> integer indicating the current water level. However, although object oriented<br />

systems are structured a round the data, we still need some procedural content to ch<strong>an</strong>ge the state of <strong>an</strong><br />

object or to make the system achieve some goal. Therefore, we also need to consider what operations a<br />

user of each object might require. Note that the emphasis here is on the user of the object <strong><strong>an</strong>d</strong> what they<br />

will require of the object rather th<strong>an</strong> what operations will be performed on the data.<br />

Let us start with the switch object. The switch state c<strong>an</strong> take a number of values such as ‘off’,<br />

‘wash’ <strong><strong>an</strong>d</strong> ‘wipe’. As we don’t w<strong>an</strong>t other objects to have direct access to this variable we must<br />

identify the services which the switch should offer. As a user of a switch we w<strong>an</strong>t to be able to move it<br />

between its various settings. As these settings are essentially <strong>an</strong> enumerated type, we c<strong>an</strong> have the<br />

concept of incrementing or decrementing the switch position. A switch must therefore provide a<br />

moveUp <strong><strong>an</strong>d</strong> a moveDown interface. Exactly how this is done will depend on the programming<br />

l<strong>an</strong>guage used. For now we will just concentrate on specifying the required facilities.<br />

If we continue examining each object <strong><strong>an</strong>d</strong> identifying the required services we could end up with the<br />

following list:<br />

switch moveUp increment switch value as above<br />

moveDown decrement switch value as above<br />

state? return a value indicating the current switch state<br />

fuse working? indicate if the fuse has blown or not<br />

wiper motor working? indicates whether the wipers are working or not<br />

pump working? indicates whether the pump is active or not<br />

water bottle fill fill the water bottle with water<br />

extract remove some water from the water bottle<br />

empty empty the water bottle<br />

Note that we have generated the list by examining each of the objects in isolation. The aim was to<br />

identify the services which might reasonably be required. We may well identify further services when<br />

we attempt to “put it all together” but for the moment we will stick to these.<br />

Each of these services should relate to a method within the object. For example, the moveUp <strong><strong>an</strong>d</strong><br />

moveDown services should relate to methods w hich ch<strong>an</strong>ge the state inst<strong>an</strong>ce variable within the<br />

object. Using a generic pseudo code, the moveUp method, within the switch object, might resemble:<br />

define method moveUp()<br />

if state = ‘off’ then<br />

state := ‘wash’<br />

elseif state = ‘wash’ then<br />

1<br />

The hexagonal shape used in this figure for inst<strong>an</strong>ces is based on the structured cloud used in version<br />

Modeling L<strong>an</strong>guage described in Part 6 of this book.<br />

0.8 of the Unified<br />

36

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

Saved successfully!

Ooh no, something went wrong!