设计模式学习笔记之(桥接模式brid…

首先桥接模式解决的是抽象和具体实现相分离的问题。想象下这种常见的场景车在路上跑,这是一个行为,但是具体是什么车之什么路上跑就跟具体的使用场景相关联了。比如说可能是宝马车之高速公路上跑,也有可能是
公交车在大街上跑,或者是你骑摩托在乡间的小路中跑,这是不一定的,但是车在路上跑这个行为是固定的,具体是是什么这个不固定,跟实际场景相关联,我们需要有一种比较灵活的方式去处理这个问题。把这个问题抽象成一个通用的模型,当新的路和车产生时我们也可以支持。不多说,直接上代码。
首先我把车给抽象出来:

package com.qunar.study.design.bridge;


public abstract class AbstractCar {

public abstract String description();


public abstract void drive();
}
然后是把路给抽象出来,并定义好车在路上跑这个行为,及具体的规范实现。代码如下:
 
  
package com.qunar.study.design.bridge;


public abstract class AbstractRoad {
protected Long speed;


public abstract Long getSpeed();


public abstract void setSpeed(Long speed);


public void run(AbstractCar abstractCar){
System.out.println(abstractCar.description());
abstractCar.drive();
System.out.println("the speed is "+getSpeed()+"km/h");
}
}
这是我们把路和车都抽象出来,那么我们看具体的实现,也就是我们现实生活中出现的场景。
首先我们车有奥迪和宝马。。。。以及各种车
 
  
package com.qunar.study.design.bridge;


public class AudiCar extends AbstractCar{
@Override
public String description() {
return "I am auti";
}

@Override
public void drive() {
System.out.println(" audi is running ");
}
}

package com.qunar.study.design.bridge;


public class BMWCar extends AbstractCar{
@Override
public String description() {
return "I am BMW,what can I do for you??";
}

@Override
public void drive() {
System.out.println(" BMW is dring in the road ");
}
}
我们的路也有各种路,高速公路,大街等,只举例这两种
package com.qunar.study.design.bridge;


public class HignSpeedRoad extends AbstractRoad{

public void highSpeed(){
System.out.println("high speed road");
}


public Long getSpeed(){
return speed;
}

public void setSpeed(Long speed){
this.speed=speed;
}
}
package com.qunar.study.design.bridge;


public class StreetRoad extends AbstractRoad{

public void runInStreet(){
System.out.println("run in street ");
}

@Override
public Long getSpeed() {
return this.speed;
}

@Override
public void setSpeed(Long speed) {
this.speed=speed;
}
}
最后看下我们实际中的case
package com.qunar.study.design.bridge;


public class RoadAndCarDriverTest {
public static void main(String[] args) {
//highspeed road run bmw car
AbstractCar bmwCar=new BMWCar();
AbstractRoad hignSpeedRoad=new HignSpeedRoad();
hignSpeedRoad.setSpeed(100L);
hignSpeedRoad.run(bmwCar);

System.out.println(" -------------------this is the boundary------------------");
//highspeed road run audi car
AbstractCar audiCar=new AudiCar();
hignSpeedRoad.run(audiCar);

System.out.println(" -------------------this is the boundary------------------");
//street run bmw car
AbstractRoad streetRoad=new StreetRoad();
streetRoad.setSpeed(60L);
streetRoad.run(bmwCar);
System.out.println(" -------------------this is the boundary------------------");
//street run audi car
streetRoad.run(audiCar);

}
}

最终的输出结果如下:
 
  


15.app/Contents/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain com.qunar.study.design.bridge.RoadAndCarDriverTest
I am BMW,what can I do for you??
 BMW is dring in the road 
the speed is 100km/h
 -------------------this is the boundary------------------
I am auti
 audi is running 
the speed is 100km/h
 -------------------this is the boundary------------------
I am BMW,what can I do for you??
 BMW is dring in the road 
the speed is 60km/h
 -------------------this is the boundary------------------
I am auti
 audi is running 
the speed is 60km/h

Process finished with exit code 0
我们可以看到,已经实现了车和路之间的随意搭配,没有写死,当你后期有新的车和路的时候,直接继承就OK了,非常灵活。
扩展性非常OK。但是目前qpx系统中哪里能用到这个模式,暂时还没有想清楚,如果后期有看到类似的需求我们可以用这个去实现。







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值