Engr 691-12: Special Topics in Engineering Science
Software Architecture
Fall Semester 2000
Lecture Notes


Mechanisms for Software Reuse (Budd's UOOPJ, Ch. 10)

This is a set of "slides" to accompany chapter 10 of Timothy Budd's textbook Understanding Object-Oriented Programming with Java (Addison-Wesley, 1998).


Inheritance and Substitutability

Idealization of inheritance:
instance of "subclass" can substitute for instance of parent

In Java, substitutability by implementing interfaces as well as subclassing

Abstract concept captured with two rules of thumb:

is-a relation
"A dog is-a mammal" sounds right

Natural for dog to inherit from mammal

has-a relation
"A car is-a(n) engine" sounds wrong

Not natural to use inheritance

But "a car has-a(n) engine" sounds right

Can use composition (aggregation)


Two Approaches to Software Reuse in OOP

inheritance
the is-a relationship

composition (aggregation)
the has-a relationship


Example -- Building Sets from Lists

Suppose already have List data type with the following behavior:

class List 
{   public List() { ... }
    public void add(int) { ... }
    public void remove(int) { ... }
    public boolean includes(int) { ... }
    public int firstElement() { ... }
    ...
}

Wish to build Set data type -- elements are unique and unordered in set


Using Inheritance

class Set extends List 
{   public void add(int) { ... }
}


Using Composition (Aggregation)

class Set 
{   public Set() { ... data = new List(); ... }
    public void add(int) { ... }
    public void remove(int) { ... }
    public boolean includes(int) { return data.includes(int); }
    private List data;
}


Comparison of Inheritance and Composition

Advantages of inheritance over composition:

Advantages of composition over inheritance:


Combining Inheritance and Composition

Java API class InputStream


Dynamic Composition

An advantage of composition over inheritance is delay in binding time -- parent/child inheritance at compile time

class Frog
{   public Frog() { behavior = new TadpoleBehavior(); }

    public void grow()   // see if behavior should change
    {   if (behavior.growUp()) { behavior = new AdultFrogBehavor(); }
        behavior.grow();  // behavior does actual work
        behavior.swim();
    }

    private FrogBehavior behavior;
}

abstract class FrogBehavior
{   public void grow();
    public void swim();
    public boolean growUp() { return false;  }
}

class TadpoleBehavior extends FrogBehavior
{   public void grow() { ... }
    public void swim() { ... }
    public boolean growUp() { age++;  return (age > 24); }

    private int age = 0;
}

class AdultFrogBehavior extends FrogBehavior
{   public void grow() { ... }
    public void swim() { ... }
}


Will Widespread Software Reuse Become Reality?

Even with the right mechanisms, software reuse not guaranteed to occur

Also need the right culture:


UP to ENGR 691 Lecture Notes root document?


Copyright © 2000, H. Conrad Cunningham
Last modified: Wed Sep 13 19:01:16 2000