JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Mediator pattern
  • With the mediator pattern communication between objects is encapsulated with a mediator object.
  • Objects no longer communicate directly with each other, but instead communicate through the mediator.
  • This reduces the dependencies between communicating objects, thereby lowering the coupling
Purpose
  • Define an object that encapsulates how a set of objects interact.
  • Mediator promotes loose coupling by keeping objects from referring to each other
    explicitly, and it lets you vary their interaction independently.
Applications
  • A set of objects communicate in well-defined but complex ways. 
  • The resulting interdependencies are unstructured and difficult to understand.
  • Rreusing an object is difficult because it refers to and communicates with many other objects.
  • A behavior that's distributed between several classes should be customizable without a lot of
    subclassing..
Examples of Mediator Pattern

  • The Mediator defines an object that controls how a set of objects interact.
  •  Loose coupling between colleague objects is achieved by having colleagues
    communicate with the Mediator, rather than with each other.

Example:-

Consumer.java
class Consumer extends Thread {
   private Mediator med;// 3. Consumers are coupled only to
   private int      id; //the Mediator
   private static int num = 1;
   public ConsumerMediator m ) { med = m;  id = num++; }
   public void run() {
      while (true) {
         System.out.print"c" + id + "-"
 
+ med.retrieveMessage() "   " );
}  }  }


Mediator.java

class Mediator {                     
   private boolean slotFull = false;  
   private int     number;
   public synchronized void storeMessageint num ) {
      while (slotFull == true)       
         try wait()catch (InterruptedException e ) { }
      slotFull = true;
      number = num;
      notifyAll();
   }
   public synchronized int retrieveMessage() {
      while (slotFull == false)      
         try wait()catch (InterruptedException e ) { }
      slotFull = false;
      notifyAll();
      return number;
}  }



Producer.java
class Producer extends Thread {
   private Mediator med;                
   private int      id;                 
   private static int num = 1;
   public ProducerMediator m ) { med = m;  id = num++; }
   public void run() {
      int num;
      while (true) {
         med.storeMessagenum = (int)(Math.random()*100) );
         System.out.print"p" + id + "-" + num + "   " );
}  }  }

MediatorDemo.java
class MediatorDemo {
   public static void mainString[] args ) {
      Mediator mb = new Mediator();
      new Producermb ).start();
      new Producermb ).start();
      new Consumermb ).start();
      new Consumermb ).start();
      new Consumermb ).start();
      new Consumermb ).start();
}  }

Output:-


Download Zip code

Example:-

DvdLowercaseTitle.java
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;
   }
}

DvdTitle.java
public abstract class DvdTitle {  
   private String title; 
   
   public void setTitle(String titleIn) {
       this.title = titleIn;
   }
   public String getTitle() {
       return this.title;
   }
}

DvdUpcaseTitle.java
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;
   }
}

DvdMediator.java
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());
   }   
}

TestDvdMediator.java
class TestDvdMediator {            
    
   public static void main(String[] args) {
       DvdMediator dvdMediator = new DvdMediator();
       DvdLowercaseTitle dvdLower = 
         new DvdLowercaseTitle("Karunakar 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());
   }
}      

Output:-


Download Zip code

Example:

AmericanSeller.java

package com.cakes;

public class AmericanSeller {

  Mediator mediator;
  float priceInDollars;

  public AmericanSeller(Mediator mediator, float priceInDollars) {
    this.mediator = mediator;
    this.priceInDollars = priceInDollars;
    this.mediator.registerAmericanSeller(this);
  }

  public boolean isBidAccepted(float bidInDollars) {
    if (bidInDollars >= priceInDollars) {
      System.out.println("Seller accepts the bid of "
 
+ bidInDollars + " dollars\n");
      return true;
    else {
      System.out.println("Seller rejects the bid of "
 
+ bidInDollars + " dollars\n");
      return false;
    }
  }

}

Buyer.java

package com.cakes;

public class Buyer {

  Mediator mediator;
  String unitOfCurrency;

  public Buyer(Mediator mediator, String unitOfCurrency) {
    this.mediator = mediator;
    this.unitOfCurrency = unitOfCurrency;
  }

  public boolean attemptToPurchase(float bid) {
    System.out.println("Buyer attempting a bid of
 " 
+ bid + " " + unitOfCurrency);
    return mediator.placeBid(bid, unitOfCurrency);
  }
}

DollarConverter.java

package com.cakes;

public class DollarConverter {

  Mediator mediator;

  public static final float DOLLAR_UNIT = 1.0f;
  public static final float EURO_UNIT = 0.7f;
  public static final float KRONA_UNIT = 8.0f;

  public DollarConverter(Mediator mediator) {
    this.mediator = mediator;
    mediator.registerDollarConverter(this);
  }

  private float convertEurosToDollars(float euros) {
    float dollars = euros * (DOLLAR_UNIT / EURO_UNIT);
    System.out.println("Converting " + euros + " euros to
 " 
+ dollars + " dollars");
    return dollars;
  }

  private float convertKronorToDollars(float kronor) {
    float dollars = kronor * (DOLLAR_UNIT / KRONA_UNIT);
    System.out.println("Converting " + kronor + " kronor
 to " 
+ dollars + " dollars");
    return dollars;
  }

  public float convertCurrencyToDollars(float amount,
 String unitOfCurrency
) {
    if ("krona".equalsIgnoreCase(unitOfCurrency)) {
      return convertKronorToDollars(amount);
    else {
      return convertEurosToDollars(amount);
    }
  }
}

FrenchBuyer.java

package com.cakes;

public class FrenchBuyer extends Buyer {

  public FrenchBuyer(Mediator mediator) {
    super(mediator, "euro");
    this.mediator.registerFrenchBuyer(this);
  }
}

Mediator.java

package com.cakes;

public class Mediator {

  Buyer swedishBuyer;
  Buyer frenchBuyer;
  AmericanSeller americanSeller;
  DollarConverter dollarConverter;

  public Mediator() {
  }

  public void registerSwedishBuyer(SwedishBuyer swedishBuyer) {
    this.swedishBuyer = swedishBuyer;
  }

  public void registerFrenchBuyer(FrenchBuyer frenchBuyer) {
    this.frenchBuyer = frenchBuyer;
  }

  public void registerAmericanSeller(AmericanSeller
 americanSeller
) {
    this.americanSeller = americanSeller;
  }

  public void registerDollarConverter(DollarConverter
 dollarConverter
) {
    this.dollarConverter = dollarConverter;
  }

  public boolean placeBid(float bid, String unitOfCurrency) {
    float dollarAmount = dollarConverter.
convertCurrencyToDollars
(bid, unitOfCurrency);
    return americanSeller.isBidAccepted(dollarAmount);
  }
}

SwedishBuyer.java

package com.cakes;

public class SwedishBuyer extends Buyer {

  public SwedishBuyer(Mediator mediator) {
    super(mediator, "krona");
    this.mediator.registerSwedishBuyer(this);
  }
}

Demo.java

package com.cakes;

public class Demo {

  public static void main(String[] args) {

    Mediator mediator = new Mediator();

    Buyer swedishBuyer = new SwedishBuyer(mediator);
    Buyer frenchBuyer = new FrenchBuyer(mediator);
    float sellingPriceInDollars = 10.0f;
    AmericanSeller americanSeller = new
 
AmericanSeller(mediator, sellingPriceInDollars);
    DollarConverter dollarConverter = new
 
DollarConverter(mediator);

    float swedishBidInKronor = 55.0f;
    while (!swedishBuyer.attemptToPurchase(swedishBidInKronor)) {
      swedishBidInKronor += 15.0f;
    }

    float frenchBidInEuros = 3.0f;
    while (!frenchBuyer.attemptToPurchase(frenchBidInEuros)) {
      frenchBidInEuros += 1.5f;
    }

  }

}

Output:-


Download Zip code