Inheritance and Dynamic Binding: idioms and common patterns

Steven Zeil

Last modified: Jun 29, 2015
Contents:

1 Inheritance Captures Variant Behavior

Why bother with inheritance & dynamic binding at all?

Because it offers a convenient mechanism for capturing variant behaviors among different groups of objects.

  string render(int width)
  Value* clone()

But to actually store a numeric value, we need a data member to hold a number (and a function via which we can retrieve it, though we’ll ignore that for the moment). Similarly, we can expect that, to store a string value, we would need a data member to store the string.

We will assume, therefore, that


A pre-OO Approach to Variant behavior

 class Value {
 public:
   enum ValueKind {Numeric, String, Error};
   Value (double dv);
   Value (string sv);
   Value ();
 
   ValueKind kind;
   double d;
   string s;
 
   string render(int width) const;
 }


A pre-OO Approach to Variant behavior

 class Value {
 public:
   enum ValueKind {Numeric, String, Error};
   Value (double dv);
   Value (string sv);
   Value ();
 
   ValueKind kind;
   double d;
   string s;
 
   string render(int width) const;
 }


Multi-Way Branching

 string Value::render(int width) const {
   switch (kind) {
     case Numeric: {
       string buffer;
       ostringstream out (buffer);
       out << dv.d;
       return buffer.substr(0,width);
       }
     case String:
       return sv.s.substr(0.width);
     case Error:
       return string("** error **").substr(0.width);
   }
 }


Variant Behavior under OO

valuerender.h
 class Value {
 public:
   virtual string render(int width) const;
 };

 class NumericValue: public Value {
 public:
   NumericValue (double dv);

   double d;
 
   string render(int width) const;
 };

 class StringValue: public Value {
 public:
   StringValue (string sv);

   string s;
 
   string render(int width) const;
 };

 class ErrorValue: public Value {
   ErrorValue ();
  
   string render(int width) const;
 };

Now, compare that with the OO approach.


Variants are Separated

valuerender.cpp
 string NumericValue::render(int width) const
 {
   string buffer;
   ostringstream out (buffer);
   out << d;
   return buffer.substr(0,width);
 }
 
 
 string StringValue::render(int width) const {
   return s.substr(0.width);
 }
 
 string ErrorValue::render(int width) const {
   return string("** error **").substr(0.width);
 }

Here’s the OO take on the same render function.


Summary

We use inheritance in our programming designs whenever we find ourselves looking at objects that

Conversely, if we don’t see that kind of variant behavior, we probably have no need for inheritance.

2 Using Inheritance

We’ll look at 3 idioms describing good ways to use inheritance.

2.1 Specialization

When inheritance is used for specialization,

The new class may therefore be substituted for a value of the parent.

This is, in many ways, the “classical” view of inheritance.


Recognizing Specialization

 

2.2 Specification

Inheritance for specification

takes place when


Defining Protocols

A protocol is a set of messages (functions) that can be used together to accomplish some desired task.


Recognizing the Specification Idiom of Inheritance


Example: Varying Data Structures

A common requirement in many libraries is to provide different data structures for the same abstraction.


libg++

 


Working with a Specialized Protocol

genset.cpp
void generateSet (int numElements, Set& s, int *expected)
{
  int elements[MaxSetElement];
  

  for (int i = 0; i < MaxSetElement; i++)
    {
      elements[i] = i;
      expected[i] = 0;
    }
  

  // Now scramble the ordering of the elements array
  for (i = 0; i < MaxSetElement; i++)
    {
      int j = rand(MaxSetElement);
      int t = elements[i];
      elements[i] = elements[j];
      elements[j] = t;
    }

  // Insert the first numElements values into s
  s.clear();
  for (i = 0; i < numElements; i++)
    {
      s.add(elements[i]);
      expected[elements[i]] = 1;
    }
}

A programmer could write code, like the code shown here, that could work an any set.

2.2.1 Abstract Base Classes


Adding to a Set Subclass

If we are working with libg++ This is OK:

void foo (Set& s, int x)
{
   s.add(x);
   cout << x << " has been added." << endl;
}
 
int main ()
{
   BSTSet s;
   foo (s, 23);
    ⋮

Adding to a General Set

But what should happen here?

 void foo (Set& s, int x)
 {
     s.add(x);
     cout << x << " has been added."   << endl;
 }
 
 int main ()
 {
     Set s;
     foo (s, 23);
       ⋮


How Do We Prevent This?

 void foo (Set& s, int x)
 {
     s.add(x);
     cout << x << " has been added."   << endl;
 }
 
 int main ()
 {
     Set s;
     foo (s, 23);
       ⋮


Abstract Member Functions

class Set {
      ⋮
    virtual Set& add (int) = 0;
      ⋮
};
   

Abstract Classes

An abstract class in C++ is any class that

“Abstract classes” are also known as pure virtual classes.


Set as an Abstract Class

Set in libg++ is a good example of a class that should be abstract.


Limitations of Abstract Classes

Abstract classes carry some limitations, designed to make sure we use them in a safe manner.

class Set {
   ⋮
  virtual Set& add (int) = 0;
   ⋮
};
  ⋮
void foo (Set& s, int x) // OK
  ⋮
 
int main () {
   Set s;  // error!
   foo (s, 23);
     ⋮


Abstract Classes & Specification

value.h
#ifndef VALUE_H
#define VALUE_H

#include <string>
#include <typeinfo>

//
// Represents a value that might be obtained for some spreadsheet cell
// when its formula was evaluated.
// 
// Values may come in many forms. At the very least, we can expect that
// our spreadsheet will support numeric and string values, and will
// probably need an "error" or "invalid" value type as well. Later we may 
// want to add addiitonal value kinds, such as currency or dates.
//
class Value
{
public:
  virtual ~Value() {}


  virtual std::string render (unsigned maxWidth) const = 0;
  // Produce a string denoting this value such that the
  // string's length() <= maxWidth (assuming maxWidth > 0)
  // If maxWidth==0, then the output string may be arbitrarily long.
  // This function is intended to supply the text for display in the
  // cells of a spreadsheet.


  virtual Value* clone() const = 0;
  // make a copy of this value

protected:
  virtual bool isEqual (const Value& v) const = 0;
  //pre: typeid(*this) == typeid(v)
  //  Returns true iff this value is equal to v, using a comparison
  //  appropriate to the kind of value.

  friend bool operator== (const Value&, const Value&);
};

inline
bool operator== (const Value& left, const Value& right)
{
  return (typeid(left) == typeid(right))
    && left.isEqual(right);
}

#endif
expression.h
#ifndef EXPRESSION_H
#define EXPRESSION_H

#include <string>
#include <iostream>

#include "cellnameseq.h"

class SpreadSheet;
class Value;

// Expressions can be thought of as trees.  Each non-leaf node of the tree
// contains an operator, and the children of that node are the subexpressions
// (operands) that the operator operates upon.  Constants, cell references,
// and the like form the leaves of the tree.
// 
// For example, the expression (a2 + 2) * c26 is equivalent to the tree:
// 
//                *
//               / \
//              +   c26
//             / \
//           a2   2

class Expression 
{
public:

  virtual ~Expression() {}


  // How many operands does this expression node have?
  virtual unsigned arity() const = 0;

  // Get the k_th operand
  virtual const Expression* operand(unsigned k) const = 0;
  //pre: k < arity()




  // Evaluate this expression
  virtual Value* evaluate(const SpreadSheet&) const = 0;



  // Copy this expression (deep copy), altering any cell references
  // by the indicated offsets except where the row or column is "fixed"
  // by a preceding $. E.g., if e is  2*D4+C$2/$A$1, then
  // e.copy(1,2) is 2*E6+D$2/$A$1, e.copy(-1,4) is 2*C8+B$2/$A$1
  virtual Expression* clone (int colOffset, int rowOffset) const = 0;



  virtual CellNameSequence collectReferences() const;


  static Expression* get (std::istream& in, char terminator);
  static Expression* get (const std::string& in);
  virtual void put (std::ostream& out) const;



  // The following control how the expression gets printed by 
  // the default implementation of put(ostream&)

  virtual bool isInline() const = 0;
  // if false, print as functionName(comma-separated-list)
  // if true, print in inline form

  virtual int precedence() const = 0;
  // Parentheses are placed around an expression whenever its precedence
  // is lower than the precedence of an operator (expression) applied to it.
  // E.g., * has higher precedence than +, so we print 3*(a1+1) but not
  // (3*a1)+1

  virtual string getOperator() const = 0;
  // Returns the name of the operator for printing purposes.
  // For constants, this is the string version of the constant value.



};



inline std::istream& operator>> (std::istream& in, Expression*& e)
{
  string line;
  getline(in, line);
  e = Expression::get (line);
  return in;
}


inline std::ostream& operator<< (std::ostream& out, const Expression& e)
{
  e.put (out);
  return out;
}

inline std::ostream& operator<< (std::ostream& out, const Expression* e)
{
  e->put (out);
  return out;
}

#endif

2.3 Extension

In this style of inheritance, a limited number of “new” abilities is grafted onto an otherwise unchanged superclass.


class FlashingString: public StringValue { bool _flash; public: FlashingString (std::string); void flash(); void stopFlashing(); };

Are Extensions OK?


Mixins

A mixin is a class that makes little sense by itself, but provides a specialized capability when used as a base class.


Mixin Example: Noisy

Noisy is a mixin I use when debugging.

noisy.h
#ifndef NOISY_H
#define NOISY_H

class Noisy 
{
  static int lastID;
  int id;
public:
  Noisy();
  Noisy(const Noisy&);
  virtual ~Noisy();

  void operator= (const Noisy&);
};

#endif
noisy.cpp
#include "noisy.h"
#include <iostream>

using namespace std;

int Noisy::lastID = 0;

Noisy::Noisy()
{
  id = lastID++;
  cerr << "Created object " << id << endl;
}

Noisy::Noisy(const Noisy& x)
{
  id = lastID++;
  cerr << "Copied object " << id << " from object " << x.id << endl;
}

Noisy::~Noisy()
{
  cerr << "Destroyed object " << id << endl;
}
  

void Noisy::operator= (const Noisy&) {}


Using Noisy

 class TreeSet
   : public Set, public Noisy
 {
      ⋮


Mixin Example: checking for memory leaks

counted.h
#ifndef COUNTED_H
#define COUNTED_H

class Counted 
{
  static int numCreated;
  static int numDestroyed;
public:
  Counted();
  Counted(const Counted&);
  virtual ~Counted();

  static void report();

};

#endif
counted.cpp
#include "counted.h"
#include <iostream>

using namespace std;

int Counted::numCreated = 0;
int Counted::numDestroyed = 0;

Counted::Counted()
{
  ++numCreated;
}

Counted::Counted(const Counted& x)
{
  ++numCreated;
}

Counted::~Counted()
{
  ++numDestroyed;
}
  


void Counted::report()
{
  cerr << "Created " << numCreated << " objects." << endl;
  cerr << "Destroyed " << numDestroyed << " objects." << endl;
}



3 The Observer Pattern

 

observer.h
#ifndef OBSERVER_H
#define OBSERVER_H

//
// An Observer can register itself with any Observable object
// cell by calling the obervable's addObserver() function. Subsequently,
// the oberver wil be notified whenever the oberver calls its 
// notifyObservers() function (usually whenever the obervable object's
// value has changed.
//
// Notification occurs by calling the notify() function declared here.

class Observable;

class Observer
{
public:
  virtual void notify (Observable* changedObject) = 0;
};
#endif
observable.h
#ifndef OBSERVABLE_H
#define OBSERVABLE_H

#include "observerptrseq.h"

// An Observable object allows any number of Observers to register
// with it. When a significant change has occured to the Observable object, 
// it calls notifyObservers() and each registered observer will be notified.
// (See also oberver.h)

class Observer;

class Observable
{
public:

  // Add and remove observers
  void addObserver (Observer* observer);
  void removeObserver (Observer* observer);

  //   For each registered Observer, call notify(this)
  void notifyObservers();

private:
  ObserverPtrSequence observers;
};


#endif
observable.cpp
#include "observable.h"
#include "observer.h"


// An Observable object allows any number of Observers to register
// with it. When a significant change has occured to the Observable object, 
// it calls notifyObservers() and each registered observer will be notified.
//

// Add and remove observers
void Observable::addObserver (Observer* observer)
{
  observers.addToFront (observer);
}


void Observable::removeObserver (Observer* observer)
{
  ObserverPtrSequence::Position p = observers.find(observer);
  if (p != 0)
    observers.remove(p);
}

//   For each registered Observer, call hasChanged(this)
void Observable::notifyObservers()
{
  for (ObserverPtrSequence::Position p = observers.front();
       p != 0; p = observers.getNext(p))
    {
      observers.at(p)->notify(this);
    }
}

3.1 Applications of Observer

3.1.1 Example: Propagating Changes in a Spreadsheet

Anyone who has used a spreadsheet has observed the way that, when one cell changes value, all the cells that mention that first cell in their formulas change, then all the cells the mention those cells change, and so on, in a characteristic “ripple” effect until all the effects of the original change have played out.

There are several ways to program that effect. One of the more elegant is to use the Observer/Observable pattern.


Cells Observe Each Other


class Cell: public Observable, Observer { public:

The idea is that cells will observe one another.


Changing a Cell Formula

putFormula.cpp
void Cell::putFormula(Expression* e)
{
  if (theFormula != 0)  
    {  ➀
      CellNameSequence oldReferences = theFormula->collectReferences();
      for (CellNameSequence::Position p = oldReferences.front();
           p != 0; p = oldReferences.getNext(p))
        {
          Cell* c = theSheet.getCell(oldReferences.at(p));
          if (c != 0)
            c->removeObserver (this);
        }
      delete theFormula;
    }
  theFormula = e; ➁
  if (e != 0) 
    {     ➂
      CellNameSequence newReferences = e->collectReferences();
      for (CellNameSequence::Position p = newReferences.front();
           p != 0; p = newReferences.getNext(p))
        {
          Cell* c = theSheet.getCell(newReferences.at(p));
          if (c != 0)
            c->addObserver (this);
        }
    }
  theSheet.cellHasNewFormula (this); ➃
}

Here’s the code that’s actually invoked to change the expression stored in a cell.

we tell the spreadsheet that this cell has a new formula. The spreadsheet keeps a queue of cells that need to be re-evaluated, and processes them one at a time.


Evaluating a Cell’s Formula

evaluateFormula.cpp
const Value* Cell::evaluateFormula()
{
  Value* newValue = (theFormula == 0)
    ? new StringValue()
    : theFormula->evaluate(theSheet);   ➀

  if (theValue != 0 && *newValue == *theValue) ➁
    delete newValue;    ➂
  else
    {    ➃
      delete theValue;
      theValue = newValue;
      notifyObservers();   ➄
    }
  return theValue;
}

Eventually the spreadsheets calls this function on our recently changed cell.


Notifying a Cell’s Observers

void Cell::notify (Observable* changedCell)
{
  theSheet.cellRequiresEvaluation (this);
}  

What does an observing cell do when it is notified? It tells the spreadsheet that it needs to be re-evaluated.

Eventually the propagation trickles to an end, as we eventually re-evaluate cells that either do not change value or that are not themselves mentioned in the formulae of any other cells.

3.1.2 Example: Observer and GUIs

 

A spreadsheet GUI contains a rectangular array of CellViews. Each CellView observes one Cell


Scrolling the Spreadsheet

 

Not every cell will have a CellView observer.


Model-View-Controller (MVC) Pattern

 


MVC Interactions

 

How do we actually accomplish this?