第22章《桥接模式》

前端之家收集整理的这篇文章主要介绍了第22章《桥接模式》前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

分类,每一种分类都有可能变化,那么就把这种多角度分离出来让他们独立变化,减少它们之间的耦合。

分类,然后在品牌下再按照功能分类

功能分类,然后每个功能下面再按照手机品牌分类

功能都是经常变化的东西。所以在实现一个手机时,将手机品牌和手机功能独立实现,而不是通过继承实现强耦合。这样在增加一个手机品牌or增加一个手机功能时,只需要添加一个类即可,曾经的代码并不需要改动,符合开放-封闭原则;且实现了手机品牌和手机功能的解耦。

功能,且品牌和功能经常发生变化。每个手机都有自己的品牌和功能。如何实现多个手机的构建过程?

代码结构图

/**

  • @Author: cxh
  • @CreateTime: 18/1/25 22:45
  • @ProjectName: JavaBaseTest
    */
    public class BrandA1 extends PhoneBrand {
    @Override
    public void run() {
    super.softWare.run();
    }
    }


/**

  • @Author: cxh
  • @CreateTime: 18/1/25 22:47
  • @ProjectName: JavaBaseTest
    */
    public class BrandA2 extends PhoneBrand {
    @Override
    public void run() {
    super.softWare.run();
    }
    }


/**

  • @Author: cxh
  • @CreateTime: 18/1/25 22:46
  • @ProjectName: JavaBaseTest
    */
    public class BrandB1 extends PhoneBrand {
    @Override
    public void run() {
    super.softWare.run();
    }
    }


/**

  • @Author: cxh

  • @CreateTime: 18/1/25 22:51

  • @ProjectName: JavaBaseTest

  • 测试类
    */
    public class Client {
    public static void main(String[] args) {
    PhoneBrand brand;

     //手机品牌A1
     System.out.println("----手机品牌A1----");
     brand=new BrandA1();
     brand.setSoftWare(new SoftWareV1());
     brand.run();
     brand.setSoftWare(new SoftWareV2());
     brand.run();
     brand.setSoftWare(new SoftWareV3());
     brand.run();
    
     //手机品牌A2
     System.out.println("----手机品牌A2----");
     brand=new BrandA2();
     brand.setSoftWare(new SoftWareV1());
     brand.run();
     brand.setSoftWare(new SoftWareV2());
     brand.run();
     brand.setSoftWare(new SoftWareV3());
     brand.run();
    
     //手机品牌A3
     System.out.println("----手机品牌A3----");
     brand=new BrandB1();
     brand.setSoftWare(new SoftWareV1());
     brand.run();
     brand.setSoftWare(new SoftWareV2());
     brand.run();
     brand.setSoftWare(new SoftWareV3());
     brand.run();

    }
    }



/**

  • @Author: cxh

  • @CreateTime: 18/1/25 22:40

  • @ProjectName: JavaBaseTest
    */
    public abstract class PhoneBrand {
    //持有软件的引用
    protected PhoneSoftWare softWare;
    public void setSoftWare(PhoneSoftWare softWare) {
    this.softWare = softWare;
    }

    //调用软件功能方法
    public abstract void run();
    }



/**

  • @Author: cxh
  • @CreateTime: 18/1/25 22:39
  • @ProjectName: JavaBaseTest
    */
    public abstract class PhoneSoftWare {
    abstract void run();
    }


/**

  • @Author: cxh
  • @CreateTime: 18/1/25 22:48
  • @ProjectName: JavaBaseTest
    */
    public class SoftWareV1 extends PhoneSoftWare {
    @Override
    void run() {
    System.out.println("手机通信录功能运行");
    }
    }


/**

  • @Author: cxh
  • @CreateTime: 18/1/25 22:49
  • @ProjectName: JavaBaseTest
    */
    public class SoftWareV2 extends PhoneSoftWare{
    @Override
    void run() {
    System.out.println("手机游戏功能运行");
    }
    }


/**

  • @Author: cxh
  • @CreateTime: 18/1/25 22:50
  • @ProjectName: JavaBaseTest
    */
    public class SoftWareV3 extends PhoneSoftWare {
    @Override
    void run() {
    System.out.println("手机拍照功能运行");
    }
    }

猜你在找的设计模式相关文章