Tải bản đầy đủ (.pdf) (10 trang)

Object-Oriented Programming - What’s It All About

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (238.72 KB, 10 trang )

Part IV
Object-Oriented
Programming
16_597043 pt04.qxd 9/20/05 2:03 PM Page 211
In this part . . .
O
bject-oriented programming is the most hyped term
in the programming world — dot-com and business-
to-business e-commerce eclipsed it for a year or two, but
their high-flying fortunes have ’er, subsided, since the
dot-com crash of 2001.
C++ claims to be object-oriented — that’s what differenti-
ated it from good ol’ C. Java is definitely object-oriented,
as are a hundred or so other languages that were invented
during the last ten years. But what is object-oriented? Do I
have it? Can I get it? Do I want it?
Part IV demonstrates the features of C# that make it
object-oriented to the core. Not only will you be program-
ming objects, but you’ll also take possession of the keys
to powerful, flexible program designs — all right here in
Part IV!
16_597043 pt04.qxd 9/20/05 2:03 PM Page 212
Chapter 10
Object-Oriented Programming —
What’s It All About?
In This Chapter

Making nachos

Reviewing the basics of object-oriented programming


Getting a handle on abstraction and classification

Understanding why object-oriented programming is important
T
his chapter answers the musical question, “What are the concepts behind
object-oriented programming and how do they differ from the functional
concepts covered in Part II of this book?”
Object-Oriented Concept #1 —
Abstraction
Sometimes when my son and I are watching football, I whip up a terribly
unhealthy batch of nachos. I dump some chips on a plate; throw on some
beans, cheese, and lots of jalapeños; and nuke the whole mess in the
microwave oven for a few minutes.
To use my microwave, I open the door, throw the stuff in, and punch a few
buttons on the front. After a few minutes, the nachos are done. (I try not to
stand in front of the microwave while it’s working lest my eyes start glowing
in the dark.)
17_597043 ch10.qxd 9/20/05 2:04 PM Page 213
Now think for a minute about all the things I don’t do to use my microwave:
ߜ I don’t rewire or change anything inside the microwave to get it to work.
The microwave has an interface — the front panel with all the buttons
and the little time display — that lets me do everything I need.
ߜ I don’t have to reprogram the software used to drive the little processor
inside my microwave, even if I cooked a different dish the last time I
used the microwave.
ߜ I don’t look inside my microwave’s case.
ߜ Even if I were a microwave designer and knew all about the inner work-
ings of a microwave, including its software, I still wouldn’t think about all
that stuff while I was using it to heat my nachos.
These are not profound observations. You can deal with only so much stress

in your life. To reduce the number of things that you deal with, you work at a
certain level of detail. In object-oriented (OO) computerese, the level of detail
at which you are working is called the level of abstraction. To introduce
another OO term while I have the chance, I abstract away the details of the
microwave’s innards.
Happily, computer scientists — and thousands of geeks — have invented object
orientation and numerous other concepts that reduce the level of complexity
at which programmers have to work. Using powerful abstractions makes the
job simpler and far less error-prone than it used to be. In a sense, that’s what
the past half century or so of computing progress has been about: managing
ever more complex concepts and structures with ever less errors.
When I’m working on nachos, I view my microwave oven as a box. (As I’m trying
to knock out a snack, I can’t worry about the innards of the microwave oven
and still follow the Cowboys on the tube.) As long as I use the microwave only
through its interface (the keypad), nothing I can do should cause the microwave
to enter an inconsistent state and crash or, worse, turn my nachos — or my
house — into a blackened, flaming mass.
Preparing functional nachos
Suppose I were to ask my son to write an algorithm for how Dad makes
nachos. After he understood what I wanted, he would probably write, “Open
a can of beans, grate some cheese, cut the jalapeños,” and so on. When he
came to the part about microwaving the concoction, he would write some-
thing like, “Cook in the microwave for five minutes” (on a good day).
That description is straightforward and complete. But it’s not the way a func-
tional programmer would code a program to make nachos. Functional pro-
grammers live in a world devoid of objects such as microwave ovens and
214
Part IV: Object-Oriented Programming
17_597043 ch10.qxd 9/20/05 2:04 PM Page 214
other appliances. They tend to worry about flow charts with their myriad

functional paths. In a functional solution to the nachos problem, the flow of
control would pass through my finger to the front panel and then to the inter-
nals of the microwave. Pretty soon, flow would be wiggling around through
complex logic paths about how long to turn on the microwave tube and
whether to sound the “come and get it” tone.
In that world of functional programming, you can’t easily think in terms of
levels of abstraction. There are no objects and no abstractions behind which
to hide inherent complexity.
Preparing object-oriented nachos
In an object-oriented approach to making nachos, I would first identify the
types of objects in the problem: chips, beans, cheese, jalapeños, and an oven.
Then, I would begin the task of modeling those objects in software, without
regard for the details of how they will be used in the final program. For exam-
ple, I can model cheese as an object pretty much in isolation from the other
objects, and then combine it with the beans, the chips, the jalapeños, and the
oven and make them interact.
While I do that, I’m said to be working (and thinking) at the level of the basic
objects. I need to think about making a useful oven, but I don’t have to think
about the logical process of making nachos — yet. After all, the microwave
designers didn’t think about the specific problem of my making a snack.
Rather, they set about solving the problem of designing and building a useful
microwave.
After I have successfully coded and tested the objects I need, I can ratchet up
to the next level of abstraction. I can start thinking at the nacho-making level,
rather than the microwave-making level.
At this point, I can pretty much translate my son’s instructions directly into
C# code.
Object-Oriented Concept #2 —
Classification
Critical to the concept of abstraction is that of classification. If I were to ask

my son, “What’s a microwave?” he would probably say, “It’s an oven that. . . .”
If I then asked, “What’s an oven?” he might reply, “It’s a kitchen appliance
that. . . .” If I then asked “What’s a kitchen appliance?” he would probably say,
“Why are you asking so many stupid questions?”
215
Chapter 10: Object-Oriented Programming — What’s It All About?
17_597043 ch10.qxd 9/20/05 2:04 PM Page 215

×