SDSU CS 696: Advanced OOP
Spring Semester, 1997
Doc 19, Memento & Mediator

To Lecture Notes Index
San Diego State University -- This page last updated Apr 29, 1997

Contents of Doc 19, Memento & Mediator


Memento slide # 1
...Motivation slide # 1
...Consequences/ Implementation slide # 4
Mediator slide # 5
...When to use the Mediator Pattern slide # 5
...Issues slide # 6


Doc 19, Memento & Mediator Slide # 1

Memento


Store an object's internal state, so the object can be restored to this state later

Motivation


Allow undo, rollbacks, etc.

An Example

class Memento
     {
     private Hashtable savedState = new Hashtable();
     
     Memento() {}; //Give some protection
     
     void setState( String stateName, Object stateValue )
          { savedState.put( stateName, stateValue );} 

     Object getState( String stateName)
          {
          return savedState.get( stateName);
          } 
          
     Object getState( String stateName, Object defaultValue )
          {
          if ( savedState.containsKey( stateName ) )
               return savedState.get( stateName);
          else
               return defaultValue;
          } 
     }

Doc 19, Memento & Mediator Slide # 2
A Class Whose state is Saved

class ComplexObject
     {
     private String name;
     private int someData;
     private Vector objectAsState = new Vector();
     
     public Memento createMemento()
          {
          Memento currentState = new Memento();
          currentState.setState( "name", name );
          currentState.setState( "someData", new Integer(someData) );
          currentState.setState( "objectAsState", objectAsState.clone() );
          return currentState;
          }
     
     public void restoreState( Memento oldState)
          {
          name = (String) oldState.getState( "name", name );
          objectAsState = (Vector) oldState.getState( "objectAsState" );
          Integer data = (Integer) oldState.getState( "someData");
          someData = data.intValue();
          }

     // Show a way to do incremental saves
     public Memento setName( String aName )
          {
          Memento deltaState = saveAState( "name", name);
          name = aName;
          return deltaState;
          }


Doc 19, Memento & Mediator Slide # 3
public void setSomeData( int value )
          {
          someData = value;
          }

private Memento saveAState( String stateName, Object stateValue )
          {
          Memento currentState = new Memento();
          currentState.setState( stateName, stateValue );
          return currentState;
          }     
     }


Doc 19, Memento & Mediator Slide # 4

Consequences/ Implementation


Preserve encapsulation boundaries

Give Memento two interfaces: wide and narrow

Let originator have access to all set/get/state of Memento

Others let hold Memento's and destroy

C++ has good support for Memento

Using Mementos might be expensive


Doc 19, Memento & Mediator Slide # 5

Mediator


A mediator is responsible for controlling and coordinating the interactions of a group of objects (not data structures)



How does this differ from a God Class?

When to use the Mediator Pattern


When a set of objects communicate in a well-defined but complex ways

When reusing an object is difficult because it refers to and communicates with many other objects

When a behavior that's distributed between several classes should be customizable without a lot of subclassing


Doc 19, Memento & Mediator Slide # 6

Issues

How do Colleagues and Mediators Communicate?

1) Explicit methods in Mediator

class DialogDirector
     {
     private Button ok;
     private Button cancel;
     private ListBox courses;
     
     public void ListBoxItemSelected()
          { blah}
     
     public void ListBoxScrolled()
          { blah }     
     etc.
     }

2) Generic change method

class DialogDirector
     {
     private Button ok;
     private Button cancel;
     private ListBox courses;
     
     public void widgetChanged( Object changedWidget)
          { 
          if ( changedWidget == ok )               blah
          else if (  changedWidget == cancel )     more blah
          else if (  changedWidget == courses )     even more blah
          }
     }
Doc 19, Memento & Mediator Slide # 7
3) Generic change method overloaded

class DialogDirector
     {
     private Button ok;
     private Button cancel;
     private ListBox courses;
     
     public void widgetChanged( Button changedWidget)
          { 
          if ( changedWidget == ok )
               blah
          else if (  changedWidget == cancel )
               more blah
          }

     public void widgetChanged( ListBox changedWidget)
          { 
          now find out how it changed and 
          respond properly          
          }
     }