Composition Relationships

 <  Day Day Up  >  

We have already seen that composition represents a part of a whole. Although the inheritance relationship is stated in terms of is-a, composition is stated in terms of has-a. We know intuitively that a car "has-a" steering wheel (see Figure 9.2).

Figure 9.2. A composition relationship.

graphics/09fig02.gif

Is-a and Has-a

Please forgive my grammar: For consistency, I will stick with "has a engine," even though "has an engine" might be grammatically correct. I do this because I want to simply state the rules as "is-a" and "has-a."


The reason for using composition is that it combines systems into less complex parts . This is a common way for people to approach problems. Studies show that even the best of us can keep, at most, seven chunks of data in our short- term memory at one time. Thus we like to use abstract concepts. Instead of saying that we have a large unit with a steering wheel, four tires, an engine, and so on, we say that we have a car. This makes it easier for us to communicate and keep things clear in our heads.

Composition also helps in other ways, such as making parts interchangeable. If all steering wheels are the same, it does not matter which steering wheel goes with which car. In software development, interchangeable parts mean reuse. In Chapters 7 and 8 of their book Object-Oriented Design in Java , Stephen Gilbert and Bill McCarty present many examples of associations and composition in much more detail. I highly recommend referencing this material for a more in-depth look into these subjects. Here we address some of the more fundamental points of these concepts and explore some variations of their examples.

 <  Day Day Up  >  


Object-Oriented Thought Process
Object-Oriented Thought Process, The (3rd Edition)
ISBN: 0672330164
EAN: 2147483647
Year: 2003
Pages: 164
Authors: Matt Weisfeld

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net