设计模式学习笔记——桥梁模式

原文:http://blog.youkuaiyun.com/hackerain/article/details/7589146

定义:

将抽象和实现解耦,使得两者可以独立地变化。(抽象角色引用实现角色,实现抽象角色的部分实现)

 

桥接模式主要是为了补充继承的缺点的,继承有什么缺点呢?即强侵入,父类有这个方法,子类也必须有这个方法,这是不可选择的,会带来扩展性的问题。比如A类有一个方法,B类继承了A类的这个方法,C类继承了B类的这个方法,如果B类要重写父类的这个方法,那么它就要修改和C类的关系,这就带来了很大的风险。


通用类图如下:



代码:

[java]  view plain copy
  1. //抽象化角色  
  2. public abstract class Abstraction {  
  3.     private Implementor implementor;  
  4.   
  5.     //这里在抽象类中增加了一个构造函数,是为了提醒子类,尽量明确的指定实现者  
  6.     public Abstraction(Implementor implementor) {  
  7.         this.implementor = implementor;  
  8.     }  
  9.   
  10.     public Implementor getImplementor() {  
  11.         return implementor;  
  12.     }  
  13.   
  14.     public void setImplementor(Implementor implementor) {  
  15.         this.implementor = implementor;  
  16.     }  
  17.   
  18.     //自身的属性和行为  
  19.     public void request(){  
  20.         this.implementor.doSomething();  
  21.     }  
  22. }  

[java]  view plain copy
  1. //抽象化角色的具体实现  
  2. public class ConcreteAbstraction1 extends Abstraction{  
  3.   
  4.     public ConcreteAbstraction1(Implementor implementor) {  
  5.         super(implementor);  
  6.     }  
  7.   
  8.     //重写父类的行为  
  9.     @Override  
  10.     public void request() {  
  11.         super.request();  
  12.         super.getImplementor().doAnything();  
  13.     }  
  14. }  
[java]  view plain copy
  1. //抽象化角色的具体实现  
  2. public class ConcreteAbstraction2 extends Abstraction{  
  3.   
  4.     public ConcreteAbstraction2(Implementor implementor) {  
  5.         super(implementor);  
  6.     }  
  7.   
  8.     //重写父类的行为  
  9.     @Override  
  10.     public void request() {  
  11.         // TODO Auto-generated method stub  
  12.         super.request();  
  13.         super.getImplementor().doAnything();  
  14.     }  
  15. }  
[java]  view plain copy
  1. //实现类的接口  
  2. public interface Implementor {  
  3.     public void doSomething();  
  4.     public void doAnything();  
  5. }  
[java]  view plain copy
  1. public class ConcreteImpl1 implements Implementor {  
  2.   
  3.     @Override  
  4.     public void doSomething() {  
  5.         System.out.println("1 do some thing...");  
  6.     }  
  7.   
  8.     @Override  
  9.     public void doAnything() {  
  10.         System.out.println("1 do any thing...");  
  11.     }  
  12.   
  13. }  
[java]  view plain copy
  1. public class ConcreteImpl2 implements Implementor {  
  2.   
  3.     @Override  
  4.     public void doSomething() {  
  5.         System.out.println("2 do some thing...");  
  6.     }  
  7.   
  8.     @Override  
  9.     public void doAnything() {  
  10.         System.out.println("2 do any thing...");  
  11.     }  
  12.   
  13. }  
[java]  view plain copy
  1. public class Client {  
  2.     public static void main(String[] args) {  
  3.         Implementor implementor2=new ConcreteImpl2();  
  4.         Abstraction abs=new ConcreteAbstraction2(implementor2);  
  5.           
  6.         abs.request();  
  7.           
  8.           
  9.         Implementor implementor1=new ConcreteImpl1();  
  10.         Abstraction abs1=new ConcreteAbstraction2(implementor1);  
  11.           
  12.         abs1.request();  
  13.     }  
  14. }  


桥梁模式的优点:

1、  抽象和实现分离

实现可以完全不受抽象的约束,不用再绑定在一个固定的抽象层次上,解决了继承的缺点。

2、优秀的扩展能力

         抽象和实现都可以独立的进行扩展。

3、  实现细节对客户的透明

客户不用关心细节的实现,它已经通过抽象层通过聚合关系完成了封装。

 

桥梁模式的使用场景:

1、  不希望或不使用使用继承的场景,例如继承层次过度,无法更细化设计颗粒;

2、抽象或接口不稳定的场景;

3、重用性要求较高的场景,设计的颗粒度越细,则被重用的可能性就越大,而采用继承则受父类的限制,不可能出现太细的颗粒度。

桥梁模式注意事项

桥梁模式的意图还是对变化的封装,尽量把可能变化的因素封装到最细、最小的逻辑单元中,避免风险扩散。进行系统设计时,发现类的继承有N层时,可以考虑使用桥梁模式。

对于比较明确不发生变化的,则通过继承来完成,若不能确定是否会发生变化,那就认为是会发生变化,则通过桥梁模式来解决。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值