181
Chapter 7
Behavioral Design
P
for automatic type checking greatly
advanced the pace and breadth of soware development. Chapter 6
explored this capacity for reuse as supported via the inheritance construct.
is chapter continues the examination of inheritance with a focus on its
support for dynamic function selection, also known as polymorphism.
We begin by dening three forms of polymorphism used in soware and
then examine subtype polymorphism in depth.
To highlight an eective design using subtype polymorphism, we
examine abstract classes and the resulting support of heterogeneous col-
lections and ease of soware maintenance. In addition to small, intuitive
examples, we dissect code from a real-world soware tool, a disassembler,
to illustrate appropriate use of abstract classes. To understand process and
cost, and to expose readers to a truly elegant soware solution, we care-
fully explain and illustrate virtual function tables. Such an examination
is unusual in a text; more oen, background processes are dismissed as
tedious. However, in the author’s experience, this knowledge successfully
reinforces an understanding of polymorphism. To assess unavoidable, but
common, design diculties, the chapter closes by looking at type intro-
spection and specic language requirements.
CHAPTER OBJECTIVES
• Dene common forms of polymorphism
• Overloading, generics, and subtyping
• Contrast static and dynamic binding
182 ◾ Software Essentials
7.1 INHERITANCE FOR FUNCTIONALITY
In the previous chapter, we examined structural design with an empha-
sis on inheritance, the hallmark construct of object-oriented design. As
noted there, the mere structural design of inheritance can be mimicked
with composition. A composing class can gain access to all the public data
and functionality of the “parent” component. If access to protected data
and functionality is desired, then a wrapper class can be dened with the
sole purpose of opening up the protected data and functionality. What
then is so important about inheritance, the major design construct of
OOD?
The true power of inheritance is not structural reuse but behavioral
modification. By defining an interface in the base class and provid-
ing variant behavior in descendant classes, a class designer can pro-
vide quite a range of functionality, all maintainable under a uniform
interface. Heterogeneous collections and substitutability, two design
touchstones of extensible code, are feasible only under a common
interface.
We examine the design of class hierarchies and how functional-
ity (behavior) can vary within class hierarchies. OOPLs support poly-
morphism, that is, dynamic binding of function calls so that a single
object handle can provide access to varying behavior at runtime. What
are the costs and benets of polymorphism? How does one design eec-
tively using polymorphism? We address these questions and also con-
sider abstract classes as the means to standardize form within a class
hierarchy.
7.2 POLYMORPHISM
Looking at the Greek roots of the word “polymorphism,” we discern its
meaning: many (poly) and form (morph). Polymorphism in soware then
• Illustrate effective use of dynamic binding
• Examine the structure and utility of virtual function tables
• Dene and demonstrate the utility of abstract classes
• Analyze production code for disassembler
• Demonstrate the use and benets of heterogeneous collections
• Assess the cost of type identication
• Identify relevant OOD principles
Get Software Essentials now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.