NBA之桥梁模式

Design Pattern:

Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently 模式:桥梁模式就是用来解耦的,遵循面向对象的开放-闭合原则

编个故事:我们篮球队要选一个人(People)去参加篮球选拔,大家都说选天翔吧(TissanLuo), 但是我篮球打得真的很烂,教练拿出了看家宝贝,curry和durant的球衣,只要穿上它就可以 投出库里的三分,使出杜兰特的暴扣,果然很牛逼,不信你看运行结果。

球衣的接口
  public interface Clothes {
  
      public void show();
  
  }
  //库里的球衣 
  public class CurryClothes implements Clothes {
      @Override
      public void show() {
          System.out.println("curry的投三分球");
      }
  }
  //杜兰特的球衣
  public class DurantClothes implements Clothes {
      @Override
      public void show() {
        System.out.println("durant的超远暴扣");
      }
  }

复制代码
穿球衣的球员抽象

public abstract class People {

    private Clothes clothes;

    public People(Clothes clothes) {
        this.clothes = clothes;
    }

    public void show(){
        clothes.show();
    }
}
    
//实不相瞒 TissanLuo 就是我  我准备穿别人的球衣了
public class TissanLuo extends People {

    public TissanLuo(Clothes clothes) {
        super(clothes);
    }

}

复制代码
比赛开始了

public class BridgeDemo {

    public static void main(String[] args) {
            People tissanLuo = new TissanLuo(new CurryClothes());
            tissanLuo.show();
    
            tissanLuo = new TissanLuo(new DurantClothes());
            tissanLuo.show();
        }
}

复制代码
结果

转载于:https://juejin.im/post/5a378128f265da431a434a96

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值