15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

How Object-Oriented Programming Works 233<br />

Main Program<br />

Figure 7-1:<br />

Dividing a<br />

program<br />

into tasks<br />

can obscure<br />

the actual<br />

purpose of a<br />

program.<br />

Room available?<br />

How many beds does the room have?<br />

Smoking or non-smoking room?<br />

What is the price?<br />

True or False<br />

1 or 2 beds available<br />

Smoking or Non-smoking<br />

$158<br />

Subprogram RoomAvailable<br />

Subprogram RoomBeds<br />

Subprogram RoomType<br />

Subprogram RoomPrice<br />

Book II<br />

Chapter 7<br />

Figure 7-2 shows an equivalent object-oriented solution to the same program<br />

where each object represents a real-world item. Rather than having a single<br />

main program controlling multiple subprograms (like one boss controlling a<br />

dozen subordinates), OOP divides a program into multiple objects that pass<br />

messages to one another (like having a bunch of workers cooperating with<br />

one another as equals).<br />

Breaking a<br />

Large Program<br />

into Objects<br />

Although both subprograms and objects solve the same problem, they use<br />

different solutions. Object-oriented <strong>programming</strong> is basically a different way<br />

of thinking about how to solve problems.<br />

Figure 7-2:<br />

OOP divides<br />

a large<br />

program<br />

into objects<br />

that behave<br />

like their<br />

real-world<br />

counterparts.<br />

Guest<br />

Room available?<br />

Front desk clerk<br />

Subprogram RoomAvailable<br />

1 or 2 beds<br />

Smoking or Non-smoking<br />

$159<br />

Front desk clerk<br />

Subprogram RoomBeds<br />

Subprogram RoomType<br />

Subprogram RoomPrice<br />

Objects aren’t an alternative to subprograms. Subprograms solve a single<br />

task. Objects just organize related subprograms together.

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

Saved successfully!

Ooh no, something went wrong!