Programming and Problem Solving with Java

(やまだぃちぅ) #1

(^178) | Selection and Encapsulation
to using it, so those details are hidden by encapsulation. As a consequence, the programmer
who implements the class doesn’t have to understand how the larger application uses it, and
the programmer who uses the class doesn’t have to think about how it is implemented.
Even when you are the programmer in both cases, abstraction simplifies your
job because it allows you to focus on different parts of the implementation in iso-
lation from each other. What seems like a huge programming problem at first be-
comes much more manageable when you break it into little pieces that you can
solve separately (the divide-and-conquer strategy introduced in Chapter 1).
Separating a class into a logical description (an interface) and an encapsulated
implementation has two additional benefits: modifiabilityand reuse.
Encapsulation enables us to modify the implementation of a class after its ini-
tial development. Perhaps we are rushing to meet a deadline, so we create a sim-
ple but inefficient implementation. Later, we can replace the implementation with
a more efficient version. The modification is undetectable by users of the class with
the exception that their applications run faster and require less memory.
If we write a class in a manner that exposes implementation details, user
code may try to exploit some of those details. If we later change the implemen-
tation, the user code would stop working. Figure 4.6 illustrates an encapsulated im-
plementation versus one that is exposed to external code.
Encapsulation also allows us to use a class in other applications. An en-
capsulated class is self-sufficient so that it doesn’t depend on declarations in
the application. It can thus be imported into different applications without re-
quiring changes to either the class or the application.
As we will see in Chapter 7, reuse also means that an encapsulated class
can be easily extended to form new related classes. For example, suppose you
work for a utility company and are developing software to manage its fleet
of vehicles. As shown in Figure 4.7, an encapsulated class that describes a ve-
hicle could be used in the applications that schedule its use and keep track
of maintenance as well as the tax accounting application that computes its
operating cost and depreciation. Each of those applications could add ex-
tensions to the vehicle class to suit its particular requirements.
Reuse is a way to save programming effort. It also ensures that objects
have the same behavior every place that they are used.
Consistent behavior helps us to avoid and detect program-
ming errors.
Of course, preparing a class that is suitable for wider
reuse requires us to think beyond the immediate situation.
The class should provide certain basic services that enable
it to be used more generally. We will look at some of these
properties later in this chapter where we revisit the design
of the Nameclass. Not every class needs to be designed for
general reuse. In many cases, we merely need a class that
has specific properties for the problem at hand, and that
won’t be used elsewhere.
Modifiability The property of
an encapsulated class definition
that allows the implementation
to be changed without having
an effect on code that uses it
(except in terms of speed or
memory space)
Reuse The ability to import a
class into code that uses it with-
out additional modification to
either the class or the user code;
the ability to extend the defini-
tion of a class
External Code
Can be
changed
without
affecting
external
code
Changes
can
affect
external
code
Encapsulated
implementation
Exposed
implementation
Figure 4.6 Encapsulated versus
Exposed Implementation
Vehicle
Use
Scheduling
Application
Vehicle
Maintenance
Scheduling
Application
Vehicle
Tax
Accounting
Application
Figure 4.7 Reuse

Free download pdf