Inheritance Vs. Composition: The Great Debate


Introduction

I want to focus your attention again on the topics of inheritance, interfaces, composition, and polymorphism — the four enablers of object-oriented design and programming. I introduced you to these topics earlier in the book in their isolated contexts but now I’d like to present them to you collectively to highlight several important issues regarding their utilization in program design. At this point in the text you should be familiar with these concepts and comfortable with Java. This will be the case especially if you’ve attempted several of the more challenging suggested projects.

Inheritance, interfaces, composition, and polymorphism are employed together to achieve an optimal object-oriented design and implementation. However, there are no guarantees that your design, and the resulting implementation, will be anything close to optimal unless you understand the ramifications of your design decisions.

The great photographer Ansel Adams so completely mastered the photographic arts that he could produce the scene he visualized by expertly manipulating every phase of the process from exposure to print. So too must you visualize the desired characteristics of the end system and effectively employ object-oriented analysis, design, and implementation techniques to achieve your goal.

In this chapter I will review the concepts and principles of inheritance and composition. I will discuss how each contributes to code reuse and offer guidance on how to choose between the two design approaches. I will then discuss the benefits of interfaces and show you how to utilize interfaces to break functional dependencies between code modules.

As you gain programming experience and begin to grasp the subtleties and nuances of good object-oriented design you will encounter programmers and architects who have adopted one particular design methodology and extol its virtues with religious fervor. In reality, there is no absolute right object-oriented design. (Although I do believe there are absolute wrong ones!) A particular design’s suitability is dictated primarily by application requirements. And although application requirements can be expected to evolve over time, a design, no matter how good, cannot be expected to graciously accommodate major shifts in application requirements that derive from a complete misunderstanding of the application’s intended purpose.

You can, however, with the right amount of forethought, create an application architecture that accommodates major and minor feature additions with little or no negative impact to existing code modules. But you must have foreseen and accounted for the requirement to extend and change the application before making your first design decision. These issues are at the heart of the material in this chapter.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

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