Читать книгу Beginning Programming All-in-One For Dummies - Wallace Wang - Страница 43

Object-Oriented Programming

Оглавление

Structured programming helps you organize and divide your program into smaller, more manageable pieces. For small to medium programs, dividing a program into smaller programs is fine, but the larger your program gets, the more smaller programs you’ll have to worry about. Eventually, computer scientists discovered that they needed another technique for dividing large programs into parts. They called this new technique object-oriented programming (often abbreviated as OOP). Object-oriented programming solves two glaring problems with structured programming: reusability and modeling.

Reusability means that you can collect smaller programs that work together, store them in a larger group called an object, and then plug those objects into different programs like LEGO building blocks. Where structured programming encourages reusability by letting you reuse subprograms, object-oriented programming encourages reusability on a larger scale by letting you reuse objects (which contain multiple smaller programs). Reusing individual subprograms is like using bricks to build a house. Reusing objects is more like using premanufactured walls to build a house.

Modeling means making the parts of a program more intuitive. One of the reasons why assembly language is so hard to understand is because manipulating data in the processor’s registers has nothing to do with solving problems like adding two numbers together. Likewise, dividing a large program into smaller tasks, using structured programming, does nothing to help you understand the actual problem the program is trying to solve.

With modeling, you divide a problem into real-life objects. If you were writing a program to control a car, one object might be the steering mechanism, another object might be the braking mechanism, and a third object might be the electrical system. By making each part of a program (each object) model a real-life object, it can be far easier to understand the purposes of the different parts of a program and how they work together.

For example, suppose you had to write a program to land a rocket on the Moon. This is how you might write this program using structured programming:

Land a rocket on the Moon Launch rocket Guide rocket through space Find a landing area on the Moon Put rocket down on landing area

So far, structured programming seems logical, but what happens when you keep dividing tasks into smaller tasks? Just focusing on the Guide rocket through space task, you might wind up with the following:

Guide rocket through space Get current coordinates Compare current coordinates with Moon coordinates Adjust direction

Dividing the Adjust direction task into smaller tasks, you might get this:

Adjust direction Identify current speed and direction Determine angle needed to steer toward the Moon Fire thrusters to change the angle of the rocket

Notice that the deeper you keep dividing tasks, the more removed you get from knowing what the main purpose of the program may be. Just by looking at the task Identify current speed and direction, you have no idea whether this task involves flying a rocket to the Moon, driving a car down a road, or controlling a walking robot to an electric outlet to recharge its batteries.

The more you divide a larger task into smaller tasks, the harder it can be to understand what problem you're even trying to solve. This gets even worse when you start writing actual program commands.

The two parts of most programs are the commands that tell the computer what to do and the data that the program manipulates. So, if you wrote a program to identify the current speed and direction of a rocket, the commands would tell the computer how to retrieve this information, and the speed and direction would be the actual data the program uses.

Essentially, program commands are separate from the data they manipulate. If one part of a program manipulates data incorrectly, the rest of the program winds up using that contaminated data and you, as the programmer, won’t know which part of the program screwed up the data. This is like sitting in a baseball game, ordering a hot dog from a vendor, and having six people pass your hot dog down to you. When you see fingerprints all over your hot dog, can you tell which person touched your food?

Beginning Programming All-in-One For Dummies

Подняться наверх