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.

services working? working? working? working?<br />

Switch<br />

Water<br />

bottle<br />

Motor Fuse Pump Relay<br />

(a)<br />

Component<br />

Switch<br />

Water<br />

bottle<br />

(aPump)<br />

(aFuse) (aRelay) (aMotor) (aSwitch) (aWaterbottle)<br />

(b)<br />

Switch<br />

Component<br />

Waterbottle<br />

(aSwitch) (aPump) (aFuse)<br />

(aRelay)<br />

Motor<br />

(aWaterbottle)<br />

(aMotor)<br />

(c)<br />

Figure 3.6: Possible class inherit<strong>an</strong>ce relationships<br />

From this table it appears that the only way in which they differ is th at they have different names.<br />

This would suggest that they are all inst<strong>an</strong>ces of a common class such as Component (see Figure 3.6.b).<br />

This class would possess <strong>an</strong> additional inst<strong>an</strong>ce variable name to simplify object ident ification.<br />

However, the problem with making them all inst<strong>an</strong>ces of a common class is that they must all behave in<br />

exactly the same way. This is not the case. We w<strong>an</strong>t the pump to start the <strong>an</strong>alysis process off when it<br />

receives the message working?. Thus the definition of working? that it possesses must be different from<br />

fuse <strong><strong>an</strong>d</strong> relay. However, in other ways it is very similar to fuse <strong><strong>an</strong>d</strong> relay. Therefore, what we w<strong>an</strong>t is<br />

for fuse <strong><strong>an</strong>d</strong> relay to be inst<strong>an</strong>ces of a class (say Component) <strong><strong>an</strong>d</strong> for pump to be <strong>an</strong> inst <strong>an</strong>ce of a class<br />

which inherits from Component (but which redefines working?). This is illustrated in Figure 3.6.c.<br />

SimulationClass<br />

name: ''<br />

traceMessages()<br />

Switches<br />

Component<br />

Waterbottle<br />

state<br />

moveUp()<br />

moveDown()<br />

state?<br />

state<br />

working?<br />

level<br />

empty()<br />

fill()<br />

extract()<br />

(fuse)<br />

(relay)<br />

(switches)<br />

Motor<br />

Pump<br />

(water bottle)<br />

working?<br />

working?<br />

(motor)<br />

(pump)<br />

class<br />

inst<strong>an</strong>ce<br />

inherit<strong>an</strong>ce<br />

inst<strong>an</strong>ce<br />

of<br />

Figure 3.7: The final class hierarchy <strong><strong>an</strong>d</strong> inst<strong>an</strong>ce diagram<br />

40

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

Saved successfully!

Ooh no, something went wrong!