Bridge


The bridge pattern has a hierarchy of abstraction classes 
(not abstract classes) access implementation classes through 
a hierarchy of implementation interfaces that parallels the 
abstraction hierarchy. Also, a one (abstraction class) to 
many (implementation) relationship is allowed for multiple 
implementations of any abstraction.

 

In this example there is no difference between the two 
implementations (pretend there is) and the hierarchy is liner. 
The hierarchy of abstractions could have been non-liner 
(tree form) just as well.




The following two classes are the hierarchy of 
abstractions.
-----------------------------------------------------
class SimpleHeater{
    private ISimpleHeaterImpl impl;

    public SimpleHeater(ISimpleHeaterImpl impl){
        this.impl = impl;
    }

    public ISimpleHeaterImpl getImpl(){
        return this.impl;
    }

    public void setHeatLevel(int temp){
        impl.setHeatLevel(temp);
    }
}
-----------------------------------------------------


-----------------------------------------------------
class HumidifierHeater extends SimpleHeater{

    public HumidifierHeater(IHumidifierHeaterImpl impl){
        super(impl);
    }

    public void setHumidityLevel(int level){
        ((IHumidifierHeaterImpl)this.getImpl()).setHumidityLevel(level);
    }
}
-----------------------------------------------------



The following two interfaces are the hierachy of 
interfaces that parallel the abstractions.
-----------------------------------------------------
public interface ISimpleHeaterImpl{
    public void setHeatLevel(int temp);
}
-----------------------------------------------------


-----------------------------------------------------
public interface IHumidifierHeaterImpl extends ISimpleHeaterImpl{
    public void setHumidityLevel(int level);   
}

-----------------------------------------------------



The following two 'frodo' classes are an implementation
of the heater interface hierarchy.
-----------------------------------------------------
class FrodoSimpleHeaterImpl implements ISimpleHeaterImpl{
    private int heatLevel;
    
    public void setHeatLevel(int temp){
        this.heatLevel = temp;
    }
}
-----------------------------------------------------


-----------------------------------------------------
class FrodoHumidifierHeaterImpl extends FrodoSimpleHeaterImpl implements IHumidifierHeaterImpl{
    
    private int humidityLevel;

    public void setHumidityLevel(int level){
        this.humidityLevel = level;
    }
}
-----------------------------------------------------


The following two 'sam' classes are an alternative 
implementation of the heater interface hierarchy.
-----------------------------------------------------
class SamSimpleHeaterImpl implements ISimpleHeaterImpl{

    private int heatLevel;
    
    public void setHeatLevel(int temp){
        this.heatLevel = temp;
    }
}
-----------------------------------------------------


-----------------------------------------------------
class SamHumidifierHeaterImpl extends SamSimpleHeaterImpl implements IHumidifierHeaterImpl{
    
    private int humidityLevel;

    public void setHumidityLevel(int level){
        this.humidityLevel = level;
    }
}
-----------------------------------------------------


The test class simply shows all instantations.
-----------------------------------------------------
class Test{

    public static void main(String[] args){
        System.out.println("class: Test, method: main");

        SimpleHeater simpleHeater;
        HumidifierHeater humidifierHeater;

        //simple heater
        simpleHeater = new SimpleHeater(new FrodoSimpleHeaterImpl());
        simpleHeater = new SimpleHeater(new SamSimpleHeaterImpl());

        //HumidifierHeater
        humidifierHeater = new HumidifierHeater(new FrodoHumidifierHeaterImpl());
        humidifierHeater = new HumidifierHeater(new SamHumidifierHeaterImpl());
    }
}
-----------------------------------------------------


Here is a tar with the above java classes in it. 
bridge.tar