Java Design Patterns .

Java Design Patterns Mediator

Mediator Overview

Passes communication between two or more objects.


DvdTitle.java - the Abstract Colleague or Mediatee

public abstract class DvdTitle {  
   private String title; 
   
   public void setTitle(String titleIn) {
       this.title = titleIn;
   }
   public String getTitle() {
       return this.title;
   }
}
To download source right-click here and "Save As...".


DvdLowercaseTitle.java - One of Two Concrete Colleagues or Mediatees

public class DvdLowercaseTitle extends DvdTitle {
   private String LowercaseTitle;
   private DvdMediator dvdMediator;
    
   public DvdLowercaseTitle(String title, DvdMediator dvdMediator) {
       this.setTitle(title);
       resetTitle();
       this.dvdMediator = dvdMediator;
       this.dvdMediator.setLowercase(this);
   }    
   
   public DvdLowercaseTitle(DvdTitle dvdTitle, 
                            DvdMediator dvdMediator) {
       this(dvdTitle.getTitle(), dvdMediator);
   }     
   
   public void resetTitle() {
       this.setLowercaseTitle(this.getTitle().toLowerCase());
   }
   public void resetTitle(String title) {
       this.setTitle(title);
       this.resetTitle();
   }
   
   public void setSuperTitleLowercase() {
       this.setTitle(this.getLowercaseTitle());
       dvdMediator.changeTitle(this);
   }
   
   public String getLowercaseTitle() {
       return LowercaseTitle;
   }
   private void setLowercaseTitle(String LowercaseTitle) {
       this.LowercaseTitle = LowercaseTitle;
   }
}
To download source right-click here and "Save As...".


DvdUpcaseTitle.java - Two of Two Concrete Colleagues or Mediatees

public class DvdUpcaseTitle extends DvdTitle {  
   private String upcaseTitle;
   private DvdMediator dvdMediator;
    
   public DvdUpcaseTitle(String title, 
                         DvdMediator dvdMediator) {
       this.setTitle(title);
       resetTitle();
       this.dvdMediator = dvdMediator;
       this.dvdMediator.setUpcase(this);
   }    
   
   public DvdUpcaseTitle(DvdTitle dvdTitle, 
                         DvdMediator dvdMediator) {
       this(dvdTitle.getTitle(), dvdMediator);
   }       
   
   public void resetTitle() {
       this.setUpcaseTitle(this.getTitle().toUpperCase());
   }
   public void resetTitle(String title) {
       this.setTitle(title);
       this.resetTitle();
   }    
   
   public void setSuperTitleUpcase() {
       this.setTitle(this.getUpcaseTitle());
       dvdMediator.changeTitle(this);       
   }
   
   public String getUpcaseTitle() {
      return upcaseTitle;
   }
   private void setUpcaseTitle(String upcaseTitle) {
       this.upcaseTitle = upcaseTitle;
   }
}
To download source right-click here and "Save As...".


DvdMediator.java - The Mediator

public class DvdMediator {
   private DvdUpcaseTitle dvdUpcaseTitle; 
   private DvdLowercaseTitle dvdLowercaseTitle;    
   
   public void setUpcase(DvdUpcaseTitle dvdUpcaseTitle) {
       this.dvdUpcaseTitle = dvdUpcaseTitle;
   } 
   
   public void setLowercase(DvdLowercaseTitle dvdLowercaseTitle) {
       this.dvdLowercaseTitle = dvdLowercaseTitle;
   }   
   
   public void changeTitle(DvdUpcaseTitle dvdUpcaseTitle) {
       this.dvdLowercaseTitle.resetTitle(dvdUpcaseTitle.getTitle());
   }
   
   public void changeTitle(DvdLowercaseTitle dvdLowercaseTitle) {
       this.dvdUpcaseTitle.resetTitle(dvdLowercaseTitle.getTitle());
   }   
}
To download source right-click here and "Save As...".


TestDvdMediator.java - testing the Mediator

class TestDvdMediator {            
    
   public static void main(String[] args) {
       DvdMediator dvdMediator = new DvdMediator();
       DvdLowercaseTitle dvdLower = 
         new DvdLowercaseTitle("Mulholland Dr.", dvdMediator);
       DvdUpcaseTitle dvdUp = 
         new DvdUpcaseTitle(dvdLower, dvdMediator);
       
       System.out.println("Lowercase LC title :" + 
                           dvdLower.getLowercaseTitle());
       System.out.println("Lowercase super title :" + 
                           dvdLower.getTitle());   
       System.out.println("Upcase UC title :" + 
                           dvdUp.getUpcaseTitle());
       System.out.println("Upcase super title :" + 
                           dvdUp.getTitle());   
       
       dvdLower.setSuperTitleLowercase();
       
       System.out.println(" ");
       System.out.println("After Super set to LC");
       System.out.println("Lowercase LC title :" + 
                           dvdLower.getLowercaseTitle());
       System.out.println("Lowercase super title :" + 
                           dvdLower.getTitle());
       System.out.println("Upcase UC title :" + 
                           dvdUp.getUpcaseTitle());
       System.out.println("Upcase super title :" + 
                           dvdUp.getTitle());
   }
} 
To download source right-click here and "Save As...".


Test Results

Lowercase LC title :mulholland dr.
Lowercase super title :Mulholland Dr.
Upcase UC title :MULHOLLAND DR.
Upcase super title :Mulholland Dr.
 
After Super set to LC
Lowercase LC title :mulholland dr.
Lowercase super title :mulholland dr.
Upcase UC title :MULHOLLAND DR.
Upcase super title :mulholland dr.

UML

UML for Mediator

References


online
Portland Pattern Repository
Books
Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides
Java Design Patterns - A Tutorial by James W. Cooper
Comments Comments are left by visitors to FluffyCat.com and may or may not be accurate.
Comment by archive on 2006-05-18 Rate this Comment

Thanks for the response. GoF is not written exactly to my liking, maybe it's because it's poorly written or maybe it's because I require "Patterns for Dummies - Comic Book Version" (probably a combination of the two). I am currently reading GoF, and I require supplemental resources (Eckels, Cooper, etc) for translation and better understanding.

For example, I think GoF really dropped the ball on the Proxy explanation/example - that really ticked me off. It's ironic that frequently the "source" sometimes can't disseminate the info (I feel the same about Bjorn Stroustrop - after reading his "bible" on C++, I just wanted to slap him around). But, I digress...

You're right about one thing, to stand the test, it must comply with the source, and I usually do the comparison after I think I understand (having seen multiple examples).

Anyway, I have found a decent and enlightening Java/plain english demo of Mediator - If you're interested:
my.execpc.com/~gopalan/design/behavioral/mediator/mediator.html This is a slightly different take on Mediator - I think I almost understand Mediator.

Incidentally, you're site did helped me with other patterns. Thanks for your site.

Comment by Larry on 2013-04-03 Rate this Comment

My take is that the Mediator is just an intermediary. It provides a level of abstraction, but no actually functionality.

Do you like the original example as presented by the Gang of Four? I find that when in doubt best way to get clarification on a GoF pattern is to go directly to the source. If you like what they wrote then you don't need to go any further. If you don't like their explanation then it's unlikely you would find anyone else's too helpful.

Comment by archive on 2006-05-18 Rate this Comment

I have a question regarding the Mediator pattern.

I was trying to determine, based on your example, how to apply the Mediator Class.
I have to say, I cannot figure out how Mediator is useful in this example. I removed all references to the Mediator object that is passed around, and the app functions the same, the Mediator is useless here - unless I'm missing something (please correct me).

Can you tell me if I'm correct in my assessment? If I'm not correct, please point out my misconception so that I could learn from this example (I have yet to see a Mediator example that demonstrates the Mediator Class's use).

 
Sign in to comment on Java Design Patterns Mediator.