01.01.2015 Views

UML Weekend Crash Course™ - To Parent Directory

UML Weekend Crash Course™ - To Parent Directory

UML Weekend Crash Course™ - To Parent Directory

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

30<br />

Friday Evening<br />

Behavior<br />

What an object can do is pretty easy to see when you’re talking about animate objects. For<br />

example, you might expect an Employee object, like <strong>To</strong>m Pender, to work, ask for time off,<br />

call in sick, complete a task, or accept an assignment. You would say that these are all<br />

things <strong>To</strong>m can do as an employee type of object, and you would include these abilities in<br />

the object’s description, for example, the Employee class.<br />

But the pencil in the jar on my desk poses a different problem. What can a pencil do It<br />

is tempting to say that a pencil can write, but is that really true It isn’t the pencil that<br />

writes, but a person who uses it to write.<br />

You’ve discovered a second type of behavior. An object must know what can be done to it.<br />

There are a lot of people who can write with the pencil, including students, teachers, programmers,<br />

designers, and analysts. <strong>To</strong> do so, every class of objects in this list would have to include<br />

a description of the “write” behavior. There are a lot of problems with this approach, including<br />

redundant maintenance, possible conflict between the different class definitions for how to<br />

“write” resulting in possible misuse of the pencil, and the fact that all these objects would<br />

need to be notified of any changes to the pencil that would affect their ability to use their<br />

“write” behavior.<br />

An object knows what it can do<br />

An object knows what can be done to it<br />

A better solution is to write one definition for the behavior in the pencil class. That way,<br />

anyone who wants to use the pencil to write goes to the pencil to find out how. Everyone<br />

gets the same implementation of the behavior, and there is only one place to make changes.<br />

Encapsulation<br />

The discussion so far leaves you with a lot of information and no way to organize it to use<br />

it effectively. Encapsulation provides the means to organize this information so that you<br />

can use it and maintain it efficiently. Here’s what this organization looks like.<br />

First, encapsulation says you need to separate everything you know about the object into<br />

two categories:<br />

What you need to know in order to use the object<br />

What you need to know in order to make the object work properly<br />

<strong>To</strong> use the object<br />

Two years ago, I taught my daughter how to drive. My first challenge was to identify for her<br />

the minimum knowledge she had to have in order to use the car.<br />

She needs to know about the ignition, the steering, the brake, the gas pedal, the<br />

gearshift, the mirrors, the gauges, and so on.<br />

Should I include the universal joints, the spark plugs, and the fuses on the list No,<br />

because she doesn’t need to know about those things in order to use the car. In fact, she has

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

Saved successfully!

Ooh no, something went wrong!