181
Chapter 7
Behavioral Design
P
  for automatic type checking greatly
advanced the pace and breadth of soware 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 dening three forms of polymorphism used in soware and
then examine subtype polymorphism in depth.
To highlight an eective design using subtype polymorphism, we
examine abstract classes and the resulting support of heterogeneous col-
lections and ease of soware maintenance. In addition to small, intuitive
examples, we dissect code from a real-world soware tool, a disassembler,
to illustrate appropriate use of abstract classes. To understand process and
cost, and to expose readers to a truly elegant soware solution, we care-
fully explain and illustrate virtual function tables. Such an examination
is unusual in a text; more oen, 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 diculties, the chapter closes by looking at type intro-
spection and specic language requirements.
CHAPTER OBJECTIVES
Dene 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 dened 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 benets of polymorphism? How does one design eec-
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 soware then
Illustrate effective use of dynamic binding
Examine the structure and utility of virtual function tables
Dene and demonstrate the utility of abstract classes
Analyze production code for disassembler
Demonstrate the use and benets of heterogeneous collections
Assess the cost of type identication
Identify relevant OOD principles

Get Software Essentials now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.