This capacity for data abstraction--the organization of data into small,
independent objects that can communicate with each other--helps software
developers break complex programming problems into many smaller, simpler
problems. Data abstraction is one of the primary benefits of OOP. By learning how
to use the member functions that control an object, a programmer can concentrate
on what the object does rather than on how it does it.
Figure 7 shows how encapsulation works. The left side of the figure shows the
appearance of a hypothetical window object of type TWindow, and the right side
shows the implementation of that object. The implementation shows some of the
members that the TWindow object provides. These include four data members that
hold the coordinates for the left side, right side, top, and bottom of the window
and several member functions that operate on the window. The member functions
define the actions that a TWindow object can respond to. The Open function opens
the window, the Close function closes it when the user clicks the box in the
upper-left corner, and the Resize function adjusts the size of the window when,
for example, the user drags its lower-right corner. NOTE
Although the TWindow objects in Figure 7 and later figures reflect the Taligent
convention of beginning with "T" for "Type," they present a highly simplified
view of window programming and are not real CommonPoint objects. In reality, the
details of window implementation depend on the development system, the role of
the window, and so on.
The Resize member function in Figure 7 has arguments that specify the window's
coordinates. The only way to change the actual coordinates from outside the
object is to call the Resize member function; the program can't do anything to
the data members directly. The data in any object can be changed only by that
object's member functions.
Encapsulation protects an object's data from accidental or inappropriate changes,
which in this case could mean that the values of the coordinates might not match
the way the window actually looks, and text or other data inside the window might
run over its edges. Because there is only one way to change a window's
coordinates--by using the Resize member function--the drawing of the window and
its location on screen are guaranteed to match. Consistency
TWindow includes several other member functions. The Zoom function is triggered
when a user clicks the box in the window's upper-right corner to enlarge the
window so it fills up the whole screen or clicks the box again to shrink it to
its original size. The Zoom function in turn calls the Resize function,
specifying coordinates that are inset slightly from the sides of the screen and
keeping track of the original coordinates so it can restore them when the user
clicks the Zoom box again.
Three member functions take care of actually drawing the window shown in Figure
7. The DrawSelf member function can be called to redraw the entire window and its
contents. The DrawSelf function in turn calls DrawFrame and DrawContents.
DrawFrame draws the window's frame and controls, and DrawContents draws its
contents, adjusting automatically for the window's current size. Data abstraction
Much like the cells that make up living organisms, to which
they are often compared, objects hide their internal details from other objects.
Just as cells communicate with each other via chemical messages that the cell
membrane recognizes and allows to pass through to the inside of the cell, objects
communicate with each other by means of messages (evoked by calling member
functions associated with each object). If one object needs to trigger some
particular action on the data within another object, it simply sends that object
the appropriate message. The message's sender doesn't need to know anything about
the recipient or its data other than the names of the member functions to which
it responds. Encapsulation
The enforcement of the data abstraction provided by objects is
called encapsulation. Just as a cell membrane protects the cell's interior from
all external chemical signals except those the membrane recognizes, encapsulation
prevents activities in the rest of the program from accidentally damaging an
object but allows other objects to interact with that object by calling its
member functions.
is ensured.
[Contents]
[Previous]
[Next]