Some Common OOD Design Patterns

Steven Zeil

Last modified: Aug 17, 2019
Contents:

Abstract: In this lesson we examine some common design patterns employed by OO programmers that can contribute to the SOLID approach and to reducing coupling in general.

We have previously introduced the idea of a design pattern, an element of design that the programming community has found useful, more abstract than the specific code that implements it.

Examples of design patterns that we have already taken note of are the Iterator and Variant Behavior patterns. In this lesson, we will explore some common patterns and examine them in terms of the SOLID principles.

1 The Observer Pattern

 

The Observer pattern is used when we need to maintain a “consistent” view of our data across many different data objects, some of which is likely to change values frequently.

There are two classes involved:

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

Here is an Observer interface. Note that there’s not a whole lot to it. It simply defines a function by which an observer may be notifyd when an observable object changes value.

Here is the Java equivalent. The Observer/Observable pattern is so common that it is part of the standard Java API, and has been for quite some time. It has some minor differences. The function is called “update” instead of “notify” and can take a second parameter, but the basic idea is the same.

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 is not much more complicated. It has functions allowing an observer to register itself for future notifications, and a utility function that the observable object calls to talk its current list of observers and notify each of them.

Possible implementation:

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);
    }
}

The Java version of Observable is similar.

1.1 Applications of Observer

1.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 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.


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.

1.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.

1.2 Observer and SOLID

2 Model-View-Controller (MVC) Pattern

 

MVC is a powerful approach to GUI construction.

This pattern has many advantages. GUI code is hard to test. Keeping the core data independent of the GUI means we can test it using unit or similar “easy” techniques. We can also change the GUI entirely without altering the core classes. For example, my implementation of the spreadsheet has both a graphic form (as shown in the prior section) and a text-only interface that can be used over a simple telnet connection.

Separating the control code means that we can test the view by driving it from a custom Control that simply issues a pre-scripted set of calls on the view and model classes

2.1 MVC Interactions

 

How do we actually accomplish this?

2.2 MVC and SOLID

3 Constructing Objects of Dynamically Selected Types

There are many situations where we need to create new objects, but the exact class that we want to use is not known at compilation time.

If that problem sounds familiar, compare it to “dynamic binding”, which comes into play when we want to call a function but the exact body that we want to use is not known at compilation time.

But, consider the problem of parsing expressions for our spreadsheet program. Let’s suppose that we have a function that has just recognized that we have an operator that is written in the form

operator ( operand )

where operand is a subexpression that we have already parsed (for the sake of simplicity in this example, we’re only going to consider operators taking a single operand), and operator could be sqrt, or abs or sin or cos or possibly any of a number of functions to be added later to the spreadsheet program. We really don’t want our expression parsing code to look like:

static public Expression parse(Expression e) {
    Expression[] operands;
    String operatorName = ...;
    ⋮
      // We have found the operatorName and parsed a single
      // operand
      Expression parsedExp;
      if (operatorName.equals("sqrt"))
          parsedExp = new SqrtOperator(operands[0]);
      else if (operatorName.equals("abs"))
          parsedExp = new AbsOperator(operands[0]);
      else if (operatorName.equals("sin"))
          parsedExp = new SineOperator(operands[0]);
      else if (operatorName.equals("cos"))
          parsedExp = new CosineOperator(operands[0]);
      else if ...
   ⋮
}

We need some way to make it easier to modify and maintain this and similar bits of code in our program that deal with creation of objects of varying types.

3.1 The Prototype Pattern

 

The Prototype pattern offers one approach to resolving this problem. It stems from the fact that, although we cannot construct objects by dynamic binding, we can copy objects via dynamic binding.

So, if we have one or more prototype objects available that illustrate the different classes that we want to create one of, we can copy that prototype object instead of trying to invoke its constructor directly.

For example, in our spreadsheet, we might simply store one example of each single-operand operator in an array, and search for and copy the one we want:

final static Expression[] prototypes = {
   new SqrtOperator(null), new AbsOperator(null), 
   new SineOperator(null), new CosineOperator(null),
   ...
};

static public Expression parse(Expression e) {
    Expression[] operands;
    String operatorName = ...;
    ⋮
      // We have found the operatorName and parsed a single
      // operand
      Expression parsedExp = null;
      for (Expression proto: prototypes) {
          if (operatorName.equals(proto.getOperatorName())) {
             parsedExp = (Expression)proto.clone();
             parsedExp.setOperand (0, operands[0]);
           break;
          }
      }
   ⋮
}

This does not actually reduce the overall coupling of the parsing function, but it probably simplify the addition of new operator types to the program.

3.2 The Factory Pattern

I once worked on a project that processed PDF documents, occasionally needing to send selected pages to an OCR (Optical Character Recognition) program to extract the text of the page into character string form.

void processPage (PDFPage page) {
    ⋮
    OCR ocr = OCRFactor.getOCR();
    String extractedText = ocr.scanPage(page);
    ⋮
}

OCR programs can be expensive, and different customers of ours had already purchased different OCR programs and were not interested in buying another just to use with our software.

Our solution was to write a generic interface for doing OCR,

public interface OCR {
    // public static boolean isInstalled();
    public String scanPage (PDFPage page);
}

then write a class implementing that interface for each OCR program. That’s straightforward enough. But how did our program know which of those OCR classes it should use when the customer ran the code?

We added a class that checked to see whether the various OCR programs were installed on the machine, and returned an object of the appropriate class:

class OCRFactory {
    if (ACMEOCR.isInstalled())
        return new ACMEOCR();
    else if (SoftCorpOCR.isInstalled())
        return new SoftCorpOCR();
    else if ...
}

This is an example of the Factory pattern.

 

You can see that this pattern avoids coupling the application to the various concrete subclasses. The factory itself still needs to know about the concrete instances, but if creation of those objects occurs in many places in the application, this is still an overall simplification.

There are a number of variations on this pattern. In the Abstract Factory pattern, a variety of different factories are provided n their own inheritance hierarchy, each implementing a different strategy for selecting concrete classes.

A variation that I find particularly useful is to add parameters to the createinstance function that help it to choose which instance to create. This is what I would do in our spreadsheet problem:

class ExpressionFactory {

    final private static Expression[] prototypes = {
       new PlusOperator(null,null), new SubtractOperator(null,null), 
       new TimesOperator(null,null), new DividesOperator(null,null), 
       new SqrtOperator(null), new AbsOperator(null), 
       new SineOperator(null), new CosineOperator(null),
       ⋮
    };

    public Expression createInstance (
        String operatorName,
        int nOperands) throws ParseError
    {
      for (Expression proto: prototypes) {
          if (operatorName.equals(proto.getOperatorName())
              && nOperands == proto.arity()) {
              return (Expression)proto.clone();
          }
      }
      throw new ParseError();
    }
 }
    ⋮
 static public Expression parse(Expression e) {
    ExpressionFactory factory = new ExpressionFactory();    
    Expression[] operands;
    String operatorName = ...;
    ⋮
      // We have found the operatorName and parsed a single
      // operand
      Expression parsedExp = factory.createInstance(operatorName, 1);
   ⋮
}

This moves all of the Expression-subclass coupling out of the Expression class, which