将抽象部分和它的实现部分分离,使它们都可以独立的变化。它的主要特点是把抽象(abstraction)与行为实现(implementation)分离开来,从而可以保持各部分的独立性以及应对它们的功能扩展。
适用性:
1.你不希望在抽象和它的实现部分之间有一个固定的绑定关系
例如这种情况可能是因为,在程序运行时刻实现部分应可以选择或者切换
2.类的抽象以及它的实现都应该可以通过生成子类的方法加以扩充
这时Bridge模式使你可以对不同的抽象接口和实现部分进行组合,并且分别对它们进行扩充
3.对一个抽象的实现部分的修改应对客户不产生影响,即客户的代码不必重新编译
4.在许多类要生成,这一种类层次结构说明你必须将一个对象分解成两部分
5.想在多个对象间共享实现(可能使用引用计数),但同时要求客户并不知道这一点
参与者:
1.Abstraction
定义抽象类的接口,维护一个指向Implementor类型对象的指针
2.RefinedAbstraction
扩充由Abstraction定义的接口
3.Implementor
定义实现类的接口,该接口可以与Abstraction的接口不一样,事实上这两个接口可以完全不同
一般来说,Implementor接口仅提供基本操作,而Abstraction则定义了基于这些基本操作的较高层次的操作
4.ConcreteImplementor
实现Implementor接口并且定义它的具体实现
一个简单的例子:
Abstraction:
public abstract class Person{
private Clothing clothing;
private String type;
public Clothing getClothing(){
return clothing;
}
public void setClothing(){
this.clothing = ClothingFactory.getClothing();
}
public void setType(Strng type){
this.type = type;
}
public String getType(){
return this.type;
}
public abstract void dress();
}
RefinedAbstraction
public class Man extends Person {
public Man(){
setType("男人");
}
public void dress(){
Clothing clothing = getClothing();
clothing.personDressCloth(this);
}
}
public class Lady extends Person {
public Lady(){
setType("女人");
}
public void dress(){
Clothing clothing = getClothing();
clothing.personDressCloth(this);
}
}
Implementor
public abstract class Clothing{
public abstract void personDressCloth(Person person);
}
ConcreteImplementor
public class Jacket extends Clothing {
public void personDressCloth(Person person){
System.out.println(person.getType() + "穿马甲");
}
}
public class Trouser extends Clothing {
public void personDressCloth(Person person){
System.out.println(person.getType() + "穿裤子");
}
}
Test:
public class Test{
public static void main(String[] args){
Person man = new Man();
Person lady = new Lady();
Clothing jacket = new Jacket();
Clothing trouser = new Trouser();
jacket.personDressCloth(man);
trouser.personDressCloth(man);
jacket.personDressCloth(lady);
trouser.personDressCloth(lady);
}
result:
男人穿马甲
男人穿裤子
女人穿马甲
女人穿裤子
第一个例子:
我们来看看怎么应用Bridge模式来设计汽车类。
抽象 - Abstraction类:汽车类及其子类:
Car:汽车总类
Truck:汽车子类 - 卡车类。
Bus:汽车子类 - 公交车类。
行为实现 - Implementor:汽车引擎设置的行为类及子类
SetCarEngine:汽车引擎的设置接口
SetCarEngine1500cc:设置1500cc引擎
SetCarEngine2000cc:设置2000cc引擎
代码:
Abstraction
public abstract class Vehicle
{
private Engine engine;
Vehicle( Engine engine )
{
this.setEngine( engine );
}
public abstract void setEngine();
public void setEngine( Engine engine )
{
this.engine = engine;
}
public Engine getEngine()
{
return engine;
}
}
public class Bus extends Vehicle
{
public Bus( Engine engine)
{
super( engine );
}
@Override
public void setEngine()
{
System.out.print("Set Bus Engine: ");
getEngine().setEngine();
}
}
public class Truck extends Vehicle
{
public Truck( Engine engine )
{
super( engine );
}
@Override
public void setEngine()
{
System.out.print("Set Truck Engine: ");
getEngine().setEngine();
}
}
public interface Engine
{
public void setEngine();
}
public class Engine2200CC implements Engine
{
public void setEngine()
{
System.out.println("engine 2200CC");
}
}
public class Engine1500CC implements Engine
{
public void setEngine()
{
System.out.println("engine 1500CC");
}
}
//测试
public class Client
{
public static void main( String[] argv )
{
Engine engine1500 = new Engine1500CC();
Engine engine2200 = new Engine2200CC();
Vehicle bus1500 = new Bus( engine1500 );
Vehicle bus2200 = new Bus( engine2200 );
bus1500.setEngine();
bus2200.setEngine();
Vehicle truck1500 = new Truck( engine1500 );
Vehicle truck2200 = new Truck( engine2200 );
truck1500.setEngine();
truck2200.setEngine();
}
}
result:
Set Bus Engine:
engine 1500CC
Set Bus Engine:
engine 2200CC
Set Truck Engine:
engine 1500CC
Set Truck Engine:
engine 2200CC
参考资料:
http://blog.youkuaiyun.com/aaaaaaaa0705/article/details/6284292