UML Weekend Crash Course™ - To Parent Directory

UML Weekend Crash Course™ - To Parent Directory UML Weekend Crash Course™ - To Parent Directory

crnarupa.singidunum.ac.rs
from crnarupa.singidunum.ac.rs More from this publisher
01.01.2015 Views

Session 3—How to Approach the UML 31 been driving for two years and still doesn’t know about them. The information that the car exposes so that someone can use the car is called the car’s interface. The interface is how you communicate to the car that you want to use one or more of the car’s behaviors. For example, when you press on the gas pedal, you’re telling the car to go faster. So in order to use an object, you need to expose the interface of the object, like the car interface in Figure 3-6. Figure 3-6 The interface for a car allows us to use the car. To make the object work properly I remember as a kid our neighbors had the old shell of a car in a field behind the house. It had brake and gas pedals, a steering wheel, a gearshift, and so on. But wish as we might, the car never responded. An interface without an implementation doesn’t do much. We could communicate with the object, but the object didn’t have any way to respond. In order to make the object work, you need to provide the mechanisms that respond to the interface. Here is where it all starts to come together. Remember back to the part about what an object knows When you press the gas pedal, the behavior (the implementation behind that interface) needs to know how the car is put together and the state of the car. An object knows about itself, so the car would know how it is put together and its current state. More than that, the knowledge must not be altered inappropriately or the behavior won’t work properly. Consequently, encapsulation tells you that the information has to be inside the object with the behavior so that you can control access to it and protect its integrity. This is why encapsulation is often called information hiding. You hide the information inside the object, where the object has complete control. For example, Figure 3-7 illustrates the hidden, internal mechanisms that make an engine work properly. In order to make the object work properly, you need to place inside the object: The implementations for each interface The data that describes the structure of the object The data that describes the current state of the object

32 Friday Evening Exhaust manifold Fuel injection system Oil filter Pulley Figure 3-7 Mounts Waterpump Alternator Belt Crankshaft pulley Defining the internal design of an object so that it will work properly Giving an object purpose If you leave encapsulation with this description, you have a bit of a problem. Many objects can have the same interface. Just think for a minute about all the objects you know of that share the interfaces accelerate, decelerate, turn, start, and stop, such as those shown in Figure 3-8. Figure 3-8 Purpose drives the design and use of an object. The go-cart and luxury car in Figure 3-8 share all these interfaces. But you would never use them both in the same way. If interface alone doesn’t adequately distinguish objects, then what else do you need in order to define an object Purpose. You need to know why that type of object exists, what it was designed for. The interface is designed to satisfy the purpose. Encapsulation summary Encapsulation of an object requires you to expose: Its purpose, so you can select the proper object for the application you have in mind Its interface, so you know how to use the object

32<br />

Friday Evening<br />

Exhaust<br />

manifold<br />

Fuel injection<br />

system<br />

Oil filter<br />

Pulley<br />

Figure 3-7<br />

Mounts<br />

Waterpump<br />

Alternator<br />

Belt<br />

Crankshaft pulley<br />

Defining the internal design of an object so that it will work properly<br />

Giving an object purpose<br />

If you leave encapsulation with this description, you have a bit of a problem. Many objects can<br />

have the same interface. Just think for a minute about all the objects you know of that share<br />

the interfaces accelerate, decelerate, turn, start, and stop, such as those shown in Figure 3-8.<br />

Figure 3-8 Purpose drives the design and use of an object.<br />

The go-cart and luxury car in Figure 3-8 share all these interfaces. But you would never use<br />

them both in the same way. If interface alone doesn’t adequately distinguish objects, then<br />

what else do you need in order to define an object Purpose. You need to know why that type<br />

of object exists, what it was designed for. The interface is designed to satisfy the purpose.<br />

Encapsulation summary<br />

Encapsulation of an object requires you to expose:<br />

Its purpose, so you can select the proper object for the application you have in mind<br />

Its interface, so you know how to use the object

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

Saved successfully!

Ooh no, something went wrong!