1.定义
将抽象部分和它的实现部分分离,使他们都可以独立的变化。
2.适用场景
1)某个类具有两个或者两个以上的维度变化,可以使用桥接模式进行分离这两种维度的变化,例如您是一个服装制造商,既生产棉服,也生产T恤衫,每种衣服既有红色的,又有绿色的,这就出现了两个变化的维护,就可以通过桥接模式实现,下面用具体代码来实现这个示例。
2)多数据库支持。如果您的应用要支持多种数据库,通过提供不同的实现类来解决的话,可以结合抽象工厂模式实现,同时这也是一个桥接模式的应用。
3.类图
[img]http://dl2.iteye.com/upload/attachment/0105/5116/85708ba1-7993-3bde-85be-3890f57db124.jpg[/img]
1)Clothes:抽象类,具有指向Color的引用。
2)TShirtClothes、CottonClothes:继承自抽象类,具体的实现类。
3)Color:Implementor,实现类的接口。Color类定义基本操作,Clothes定义较高层次的操作。
4)RedColor、GreenColor:Implementor的具体实现。
5)BridgeMain:客户端。
这样就定义了两个维度的变化,一个是颜色的变化,一个是衣服类型的变化,而如果用集成来实现的话,就需要4个子类,如果再加一个蓝颜色的衣服的话,又得增加2个子类。而使用桥接模式,可以在这两个维度分别扩充,而互不影响。
4.代码示例
public interface Color {
public void colour();
}
public class GreenColor implements Color {
@Override
public void colour() {
System.out.println("绿色");
}
}
public class RedColor implements Color {
@Override
public void colour() {
System.out.println("红色");
}
}
public abstract class Clothes {
public Color color;
public Clothes(Color color){
this.color = color;
}
public abstract void makeClothes();
}
public class CottonClothes extends Clothes {
public CottonClothes(Color color){
super(color);
}
@Override
public void makeClothes() {
color.colour();
System.out.println("生产棉服");
}
}
public class TShirtClothes extends Clothes {
public TShirtClothes(Color color){
super(color);
}
@Override
public void makeClothes() {
color.colour();
System.out.println("生产T恤衫");
}
}
public class BridgeMain {
public static void main(String[] args) throws Exception{
//生产绿色棉服
CottonClothes clothes = new CottonClothes(new GreenColor());
clothes.makeClothes();
System.out.println("-----------------------");
//生产红色T恤衫
TShirtClothes tShirtClothes = new TShirtClothes(new RedColor());
tShirtClothes.makeClothes();
}
}
输出结果:
绿色
生产棉服
-----------------------
红色
生产T恤衫
5.同Adapter模式的区别
Adapter通常在设计完成后才被使用,属于事后诸葛亮,Bridge是在系统开始时就使用,使得抽象接口和实现可以独立的进行变化。
将抽象部分和它的实现部分分离,使他们都可以独立的变化。
2.适用场景
1)某个类具有两个或者两个以上的维度变化,可以使用桥接模式进行分离这两种维度的变化,例如您是一个服装制造商,既生产棉服,也生产T恤衫,每种衣服既有红色的,又有绿色的,这就出现了两个变化的维护,就可以通过桥接模式实现,下面用具体代码来实现这个示例。
2)多数据库支持。如果您的应用要支持多种数据库,通过提供不同的实现类来解决的话,可以结合抽象工厂模式实现,同时这也是一个桥接模式的应用。
3.类图
[img]http://dl2.iteye.com/upload/attachment/0105/5116/85708ba1-7993-3bde-85be-3890f57db124.jpg[/img]
1)Clothes:抽象类,具有指向Color的引用。
2)TShirtClothes、CottonClothes:继承自抽象类,具体的实现类。
3)Color:Implementor,实现类的接口。Color类定义基本操作,Clothes定义较高层次的操作。
4)RedColor、GreenColor:Implementor的具体实现。
5)BridgeMain:客户端。
这样就定义了两个维度的变化,一个是颜色的变化,一个是衣服类型的变化,而如果用集成来实现的话,就需要4个子类,如果再加一个蓝颜色的衣服的话,又得增加2个子类。而使用桥接模式,可以在这两个维度分别扩充,而互不影响。
4.代码示例
public interface Color {
public void colour();
}
public class GreenColor implements Color {
@Override
public void colour() {
System.out.println("绿色");
}
}
public class RedColor implements Color {
@Override
public void colour() {
System.out.println("红色");
}
}
public abstract class Clothes {
public Color color;
public Clothes(Color color){
this.color = color;
}
public abstract void makeClothes();
}
public class CottonClothes extends Clothes {
public CottonClothes(Color color){
super(color);
}
@Override
public void makeClothes() {
color.colour();
System.out.println("生产棉服");
}
}
public class TShirtClothes extends Clothes {
public TShirtClothes(Color color){
super(color);
}
@Override
public void makeClothes() {
color.colour();
System.out.println("生产T恤衫");
}
}
public class BridgeMain {
public static void main(String[] args) throws Exception{
//生产绿色棉服
CottonClothes clothes = new CottonClothes(new GreenColor());
clothes.makeClothes();
System.out.println("-----------------------");
//生产红色T恤衫
TShirtClothes tShirtClothes = new TShirtClothes(new RedColor());
tShirtClothes.makeClothes();
}
}
输出结果:
绿色
生产棉服
-----------------------
红色
生产T恤衫
5.同Adapter模式的区别
Adapter通常在设计完成后才被使用,属于事后诸葛亮,Bridge是在系统开始时就使用,使得抽象接口和实现可以独立的进行变化。