Controlling state change
The state of an object is often carefully controlled
by only allowing certain variables of an object to be accessed by the object's own
‘get’ and ‘set’ methods. This prevents some other object changing an
object's attribute to some invalid or meaningless value. For example:
as a part of a picture of a snowman a drawing
application might have a Circle object, and wish to make the radius of the circle smaller
each time some button is clicked. If the application were to simply subtract a value (say
50 pixels) from the value of the object's
radius attribute each time the button were clicked, after enough
clicks the Circle object would have a negative radius – clearly not something
a robust program should allow to happen.
This can be avoided by making the radius attribute only
changeable by the object itself, and requiring other objects (e.g. the application object)
to request a change of the radius via a set message. A Circle object can then the setRadius() operation defined
carefully so that it will never set the radius to less than 0 (or some reasonable value
for the system).
Back
to top

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg
|