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.

Session 3—How to Approach the <strong>UML</strong> 31<br />

been driving for two years and still doesn’t know about them. The information that the car<br />

exposes so that someone can use the car is called the car’s interface. The interface is how<br />

you communicate to the car that you want to use one or more of the car’s behaviors. For<br />

example, when you press on the gas pedal, you’re telling the car to go faster.<br />

So in order to use an object, you need to expose the interface of the object, like the car<br />

interface in Figure 3-6.<br />

Figure 3-6<br />

The interface for a car allows us to use the car.<br />

<strong>To</strong> make the object work properly<br />

I remember as a kid our neighbors had the old shell of a car in a field behind the house. It<br />

had brake and gas pedals, a steering wheel, a gearshift, and so on. But wish as we might,<br />

the car never responded. An interface without an implementation doesn’t do much. We could<br />

communicate with the object, but the object didn’t have any way to respond.<br />

In order to make the object work, you need to provide the mechanisms that respond to<br />

the interface. Here is where it all starts to come together. Remember back to the part about<br />

what an object knows When you press the gas pedal, the behavior (the implementation<br />

behind that interface) needs to know how the car is put together and the state of the car.<br />

An object knows about itself, so the car would know how it is put together and its current<br />

state.<br />

More than that, the knowledge must not be altered inappropriately or the behavior won’t<br />

work properly. Consequently, encapsulation tells you that the information has to be inside<br />

the object with the behavior so that you can control access to it and protect its integrity.<br />

This is why encapsulation is often called information hiding. You hide the information inside<br />

the object, where the object has complete control. For example, Figure 3-7 illustrates the<br />

hidden, internal mechanisms that make an engine work properly.<br />

In order to make the object work properly, you need to place inside the object:<br />

The implementations for each interface<br />

The data that describes the structure of the object<br />

The data that describes the current state of the object

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

Saved successfully!

Ooh no, something went wrong!