Java Design Patterns .

Java Design Patterns Bridge

Bridge Overview

An abstraction and implementation are in different class hierarchies.


Soda.java - the Abstract Base Class

public abstract class Soda {  
   SodaImp sodaImp; 
   
   public void setSodaImp() {
       this.sodaImp = SodaImpSingleton.getTheSodaImp();
   }
   public SodaImp getSodaImp() {
       return this.sodaImp;
   }
   
   public abstract void pourSoda();
}
To download source right-click here and "Save As...".


MediumSoda.java - one of two classes extending the Abstract

public class MediumSoda extends Soda {  
   public MediumSoda() {
       setSodaImp();
   }
   
   public void pourSoda() {
       SodaImp sodaImp = this.getSodaImp();
       for (int i = 0; i < 2; i++) {
           System.out.print("...glug...");
           sodaImp.pourSodaImp();
       }
       System.out.println(" ");
   }
}
To download source right-click here and "Save As...".


SuperSizeSoda.java - two of two classes extending the Abstract

public class SuperSizeSoda extends Soda {  
   public SuperSizeSoda() {
       setSodaImp();
   }
   
   public void pourSoda() {
       SodaImp sodaImp = this.getSodaImp();
       for (int i = 0; i < 5; i++) {
           System.out.print("...glug...");
           sodaImp.pourSodaImp();
       }
       System.out.println(" ");       
   }
}
To download source right-click here and "Save As...".


SodaImp.java - the Implementation Base Class

public abstract class SodaImp {  
   public abstract void pourSodaImp();
}
To download source right-click here and "Save As...".


CherrySodaImp.java - one of three classes extending the Implementation Base Class

public class CherrySodaImp extends SodaImp {
   CherrySodaImp() {}
    
   public void pourSodaImp() {
       System.out.println("Yummy Cherry Soda!");
   }
}
To download source right-click here and "Save As...".


GrapeSodaImp.java - two of three classes extending the Implementation Base Class

public class GrapeSodaImp extends SodaImp {
   GrapeSodaImp() {}
    
   public void pourSodaImp() {
       System.out.println("Delicious Grape Soda!");
   }
}
To download source right-click here and "Save As...".


OrangeSodaImp.java - three of three classes extending the Implementation Base Class

public class OrangeSodaImp extends SodaImp {  
   OrangeSodaImp() {}
    
   public void pourSodaImp() {
       System.out.println("Citrusy Orange Soda!");
   }
}
To download source right-click here and "Save As...".


SodaImpSingleton.java - a Singleton to hold the current SodaImp

public class SodaImpSingleton {  
    private static SodaImp sodaImp;
   
    public SodaImpSingleton(SodaImp sodaImpIn) {
        this.sodaImp = sodaImpIn;
    }
    
    public static SodaImp getTheSodaImp() {
        return sodaImp;
    }
}
To download source right-click here and "Save As...".


TestBridge.java - testing the Bridge

class TestBridge {
   public static void testCherryPlatform() {
       SodaImpSingleton sodaImpSingleton = 
         new SodaImpSingleton(new CherrySodaImp());
       System.out.println(
         "testing medium soda on the cherry platform");
       MediumSoda mediumSoda = new MediumSoda();
       mediumSoda.pourSoda();
       System.out.println(
         "testing super size soda on the cherry platform");
       SuperSizeSoda superSizeSoda = new SuperSizeSoda();
       superSizeSoda.pourSoda();       
   }
   
   public static void testGrapePlatform() {
       SodaImpSingleton sodaImpSingleton = 
         new SodaImpSingleton(new GrapeSodaImp());
       System.out.println(
         "testing medium soda on the grape platform");
       MediumSoda mediumSoda = new MediumSoda();
       mediumSoda.pourSoda();
       System.out.println(
         "testing super size soda on the grape platform");
       SuperSizeSoda superSizeSoda = new SuperSizeSoda();
       superSizeSoda.pourSoda();       
   }   
   
   public static void testOrangePlatform() {
       SodaImpSingleton sodaImpSingleton = 
         new SodaImpSingleton(new OrangeSodaImp());
       System.out.println(
         "testing medium soda on the orange platform");
       MediumSoda mediumSoda = new MediumSoda();
       mediumSoda.pourSoda();
       System.out.println(
         "testing super size soda on the orange platform");
       SuperSizeSoda superSizeSoda = new SuperSizeSoda();
       superSizeSoda.pourSoda();       
   }
    
   public static void main(String[] args) {
        testCherryPlatform();
        testGrapePlatform();
        testOrangePlatform();
   }
}
To download source right-click here and "Save As...".


Test Results

testing medium soda on the cherry platform
...glug...Yummy Cherry Soda!
...glug...Yummy Cherry Soda!
 
testing super size soda on the cherry platform
...glug...Yummy Cherry Soda!
...glug...Yummy Cherry Soda!
...glug...Yummy Cherry Soda!
...glug...Yummy Cherry Soda!
...glug...Yummy Cherry Soda!
 
testing medium soda on the grape platform
...glug...Delicious Grape Soda!
...glug...Delicious Grape Soda!
 
testing super size soda on the grape platform
...glug...Delicious Grape Soda!
...glug...Delicious Grape Soda!
...glug...Delicious Grape Soda!
...glug...Delicious Grape Soda!
...glug...Delicious Grape Soda!
 
testing medium soda on the orange platform
...glug...Citrusy Orange Soda!
...glug...Citrusy Orange Soda!
 
testing super size soda on the orange platform
...glug...Citrusy Orange Soda!
...glug...Citrusy Orange Soda!
...glug...Citrusy Orange Soda!
...glug...Citrusy Orange Soda!
...glug...Citrusy Orange Soda!

UML

UML for Bridge

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
Sign in to be the first to comment on Java Design Patterns Bridge.