13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

284Chapter 12Analysis and Design: Seeking the Objects12.6ESSENTIAL AND NONESSENTIALSo far, we have walked you through a very simple example, and we have madesound choices at every step. In more complex cases, even the best of us willmake mistakes. We will head down blind alleys. We will group things togetherthat might bel<strong>on</strong>g in separate abstract categories, but should, perhaps, share aninterface. These are not so much errors as judgment calls, and skill at recognizingthem and making the correct decisi<strong>on</strong>s comes <strong>on</strong>ly with experience.For now, the most important questi<strong>on</strong>s to ask include:• Do I need this class?We are often tempted to create too many inherited classes. When weseek more generic, higher level abstracti<strong>on</strong>s, it is often possible to use <strong>on</strong>lythe more abstract class. Of course, it is possible to carry that tendency toofar. If your methods c<strong>on</strong>tain a lot of “if’s” to handle various subtypes, thatmight be a case where you should inherit and overload the method.• Should I get functi<strong>on</strong>ality by inheritance or compositi<strong>on</strong>?Inheritance should be reserved <strong>on</strong>ly for cases where a class is a morespecific variety of the base class. For example, you might have a Pers<strong>on</strong>class, and then you might have Employee and Customer classes inheritcomm<strong>on</strong> attributes and methods from Pers<strong>on</strong>. This is frequently called an“is-a” relati<strong>on</strong>ship, as in “A User is a Pers<strong>on</strong>.” If your proposed inheritancerelati<strong>on</strong>ship makes sense phrased that way, it might well be a goodcandidate for inheritance.Compositi<strong>on</strong> is when you use a class as an attribute. To extend ourexample, you might have an Address class. You might be tempted to havePers<strong>on</strong> inherit from Address. But a Pers<strong>on</strong> is not an Address. Try it: “APers<strong>on</strong> is an Address.” Nope. Instead, you should just have an instance ofthe Address class as an attribute of Pers<strong>on</strong>. Such a relati<strong>on</strong>ship is oftencalled a “has-a” relati<strong>on</strong>ship, as in “A Pers<strong>on</strong> has an Address.” If therelati<strong>on</strong>ship makes sense phrased that way, it is a good candidate forcompositi<strong>on</strong>. Another way to recognize that you’ve wr<strong>on</strong>gly used inheritanceis if you end up having a radically different class inherit from thesame base class. For example, suppose you have a class, Building. Wouldit make sense for Building and Pers<strong>on</strong> to inherit from Address? AreBuildings and Pers<strong>on</strong>s more specific instances of the same general type ofthing? No, they are not. Building and Pers<strong>on</strong> should get Addressfuncti<strong>on</strong>ality by compositi<strong>on</strong>.

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

Saved successfully!

Ooh no, something went wrong!