SDSU CS 635 Advanced Object-Oriented Design & Programming
Spring Semester, 2002
Mediator & Type Object
Previous    Lecture Notes Index        
© 2002, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 02-May-02

References

Design Patterns: Elements of Resuable Object-Oriented Software, Gamma, Helm, Johnson, Vlissides, Addison Wesley, 1995, pp. 273-282

Type Object, Ralph Johnson & Bobby Woolf in Pattern Languages of Program Design 3, Edited by Martin, Riehle, Buschmann, 1998, pp. 47-65


Doc 18, Mediator & Type Object Slide # 2

Mediator


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

Structure

Classes



Objects




Doc 18, Mediator & Type Object Slide # 3
Participants

Mediator

Defines an interface for communicating with Colleague objects

ConcreteMediator

Implements cooperative behavior by coordinating Colleague objects

Knows and maintains its colleagues

Colleague classes

Each Colleague class knows its Mediator object

Each colleague communicates with its mediator whenever it would have otherwise communicated with another colleague




Doc 18, Mediator & Type Object Slide # 4

Motivating Example

Dialog Boxes

Objects



Interaction



How does this differ from a God Class?

Doc 18, Mediator & Type Object Slide # 5
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 18, Mediator & Type Object 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 18, Mediator & Type Object 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
      }
   }



Doc 18, Mediator & Type Object Slide # 8
Differences from Facade

Facade does not add any functionality, Mediator does

Subsystem components are not aware of Facade

Mediator's colleagues are aware of Mediator and interact with it

Doc 18, Mediator & Type Object Slide # 9

Type Object


Intent

Decouples instances from their classes so those classes can be implemented as instances of a class



Also Known As



Doc 18, Mediator & Type Object Slide # 10
Motivation

Video Rental Store Inventory

Need to keep track of all the movies in the inventory

What

Subclassing does not Work


What happens when new movies come out?

Instances of Videotape do not Work

Using one instance of Videotape class per movie


Using one instance of Videotape for each copy of a movie



Doc 18, Mediator & Type Object Slide # 11
Type Object Solution

Class Structure



Object Structure



Doc 18, Mediator & Type Object Slide # 12
Type Object Structure


TypeClass (Movie)


TypeObject (SpiderMan, Monsoon Wedding)



Copyright ©, All rights reserved.
2002 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.

Previous    visitors since 02-May-02