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.

een to ease the tr<strong>an</strong>sition from one approach to <strong>an</strong>other. The result has often been a less th<strong>an</strong><br />

satisfactory state of affairs. Not only does it me<strong>an</strong> that m<strong>an</strong>y software developers have moved to their<br />

new object oriented l<strong>an</strong>guage believing that it is just a matter of learning the new syntax, (it isn’t), they<br />

have proceeded t o write procedural programs in which the use of objects is limited to holding data,<br />

believing that this will be sufficient (it won’t). It is really only safe to move to a hybrid l<strong>an</strong>guage once<br />

you have learnt about object technology using a pure object oriented l<strong>an</strong>guage.<br />

1.5 Fundamentals of object orientation<br />

The object oriented programmer’s view of traditional procedural programming is of procedures wildly<br />

attacking data which is defenseless <strong><strong>an</strong>d</strong> has no control over what the procedures do to it. This has been<br />

called the rape <strong><strong>an</strong>d</strong> pillage style of programming. The object oriented programmers view of object<br />

oriented programming is of polite <strong><strong>an</strong>d</strong> well behaved data objects passing messages to one <strong>an</strong>other, each<br />

data object deciding for itself whether to accept the message <strong><strong>an</strong>d</strong> how to interpret what it me<strong>an</strong>s.<br />

The basic idea is that a system is seen as a set of interacting objects which are org<strong>an</strong>ized into classes<br />

(of objects). For example, Figure 1.2 illustrates a (simplified) cruise co ntrol system from a car. The<br />

figure illustrates the objects in the system, the links between the objects <strong><strong>an</strong>d</strong> the direction that<br />

information flows along these links. The object oriented implementation of this system would mirror<br />

this diagram exactly. That is, there would be <strong>an</strong> object representing each box. Between the boxes would<br />

be links allowing one object to request a service from <strong>an</strong>other, or provide information to <strong>an</strong>other. For<br />

example, the cruise control electronic control unit (ECU) might request the c urrent speed from the<br />

speed sensor. It would then use this information when asking the throttle to adjust its position. Notice<br />

we do not talk about functions or procedures which access information from data structures <strong><strong>an</strong>d</strong> then<br />

call other functions <strong><strong>an</strong>d</strong> proc edures. There is no concept such as the ECU data structure <strong><strong>an</strong>d</strong> the ECU<br />

main program. This c<strong>an</strong> be a difficult ch<strong>an</strong>ge of emphasis for some people <strong><strong>an</strong>d</strong> we shall try to illustrate<br />

it further below.<br />

Ignition<br />

Switch<br />

On / Off<br />

Switch<br />

Cruise Control<br />

Switches<br />

Resume/ C<strong>an</strong>cel<br />

Switch<br />

Acc / Decelerate<br />

Switch<br />

Brake Switch<br />

Clutch Switch<br />

Cruise Control Electronic<br />

Control Unit<br />

Throttle<br />

Speed Sensor<br />

Figure 1.2: A cruise control system as a set of objects<br />

The aim in object oriented programming is to shift the focus of attention from procedures that do<br />

things to data round to data which is asked to do things . The task is not to define the proce dures which<br />

will m<strong>an</strong>ipulate data but to define data objects, their attributes <strong><strong>an</strong>d</strong> the way in which they may be<br />

examined or ch<strong>an</strong>ged. Data objects (<strong><strong>an</strong>d</strong> procedures) c<strong>an</strong> communicate with data objects only through<br />

narrow, well defined ch<strong>an</strong>nels.<br />

1.6 The four catechism of object orientation<br />

The four catechism of object oriented programming are:<br />

Encapsulation or data hiding. Encapsulation is the process of hiding all the details of <strong>an</strong> object that do<br />

not contribute to its essential characteristics . Essentially, it me<strong>an</strong>s that what is inside the class is<br />

hidden; only the external interfaces are known (by other objects). That is, as the user of <strong>an</strong> object<br />

you should never need to look inside the box!<br />

Inherit<strong>an</strong>ce. In m<strong>an</strong>y cases obje cts may have similar (but not identical) properties. One way of<br />

m<strong>an</strong>aging (classifying) such properties is to have a hierarchy of classes. In this hierarchy of<br />

18

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

Saved successfully!

Ooh no, something went wrong!