15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

136<br />

Dividing a Program into Objects<br />

User Interface<br />

Changing this subprogram . . .<br />

Figure 1-7:<br />

Changing<br />

one part of a<br />

program<br />

can affect<br />

other parts<br />

of that same<br />

program.<br />

Examine hard disk<br />

Displaying report<br />

Printing<br />

Save report<br />

. . . could affect this subprogram,<br />

which can affect . . .<br />

. . . all of these other subprograms.<br />

When subprograms are highly dependent on each other, they’re high, strong,<br />

or tight coupling. When subprograms aren’t dependent on each other, they’re<br />

low, weak, or loose coupling. You want your subprograms to have low, weak,<br />

or loose coupling. That way, changing one part of your program doesn’t accidentally<br />

affect another part of your program.<br />

To en<strong>for</strong>ce weak coupling, computer scientists have created object-oriented<br />

<strong>programming</strong> (OOP). The main idea behind OOP is to divide a large program<br />

into objects.<br />

Objects act like “super” subprograms. Whereas subprograms give programmers<br />

the choice of making a tightly or loosely coupled subprogram, objects<br />

<strong>for</strong>ce programmers to create loosely coupled subprograms.<br />

By <strong>for</strong>cing programmers to create loosely coupled subprograms, objects<br />

make it easy to modify a program without worrying about any unintended<br />

side effects. OOP lets you yank out an object, modify it, and plug it back in<br />

without worrying if your changes might affect the rest of the program.<br />

Objects offer another advantage over subprograms. Whereas subprograms<br />

typically represent a specific action, objects represent specific physical<br />

items in the real world.<br />

For example, if you’re writing a program to control a robot, you could divide<br />

the program into the following subprograms that make the robot:<br />

✦ Move<br />

✦ Sense obstacles (through sight and touch) in its way<br />

✦ Navigate

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

Saved successfully!

Ooh no, something went wrong!