Java设计模式之桥接模式实例详解

时间:2019-04-07
本文章向大家介绍Java设计模式之桥接模式实例详解,主要包括Java设计模式之桥接模式实例详解使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

本文实例讲述了Java设计模式之桥接模式。分享给大家供大家参考,具体如下:

概念:

桥接模式(Bridge Pattern):将抽象部分与它的实现部分分离,使它们都可以独立地变化。

桥接模式将继承关系转换为关联关系,从而降低了类与类之间的耦合,减少了代码编写量。

什么情况下会用桥接模式?

简单的说就是我们在抽象对象的特征时,对象的特征属性又很抽象,不得不把属性再次抽象。

否则的话,具体子类的数量将会成几何增长,而且不易扩展。没办法维护现有代码。

举例,我们在抽象手机这二个对象时,它的几个属性,如操作系统,cpu,屏幕,运营商网络等都很复杂。我们不能简单的把这几个属性直接定义,必须再次抽象化。而具体的一个手机对象就是这些属性的组合,但不是简单的组合,属性需要实现自己作为属性的功能。在这样的设计下,代码的维护和扩展也就容易了。

注意:在说这个模式的时候,我不能保证说的和写得例子都是正确的,毕竟我也是新接触到,所有例子均基于与个人理解。

我认为的桥接模式说明图:

下面是例子:

1. 首先定义抽象类,抽象和描述对象的特征。

在对象的属性上划分维度,为了以后桥接和扩展。

package test.design.bridge;
public abstract class CellPhone {
  private String cellPhoneName;
  public CellPhoneSystem cellPhoneSystem;
  public CellPhoneCPU cellPhoneCPU;
  public void works(){
    System.out.println("---------------------");
    System.out.println("This cellphone is:"+this.getCellPhoneName()+",welcome to use. ");
    System.out.println("This cellphone detail infomation:");
    System.out.println("系统类型:"+this.getCellPhoneSystem().getSystemName());
    System.out.println("cpu型号:"+this.getCellPhoneCPU().getCpuName());
    System.out.println("---------------------");
  }
  public String getCellPhoneName() {
    return cellPhoneName;
  }
  public void setCellPhoneName(String cellPhoneName) {
    this.cellPhoneName = cellPhoneName;
  }
  public CellPhoneSystem getCellPhoneSystem() {
    return cellPhoneSystem;
  }
  public void setCellPhoneSystem(CellPhoneSystem cellPhoneSystem) {
    this.cellPhoneSystem = cellPhoneSystem;
  }
  public CellPhoneCPU getCellPhoneCPU() {
    return cellPhoneCPU;
  }
  public void setCellPhoneCPU(CellPhoneCPU cellPhoneCPU) {
    this.cellPhoneCPU = cellPhoneCPU;
  }
}

2. 属性维度的抽象。(可以使用接口定义,关键看你的具体功能)

package test.design.bridge;
/**
 * 属性cpu被抽象成一个维度,为了以后扩展
 * @author lushuaiyin
 *
 */
public abstract class CellPhoneCPU {
  public CellPhone cellPhone;
  public String cpuName;
  public void cpuWorks(){
    System.out.println("I am cpu. My pattern is:"+this.getCpuName());
    System.out.println("I am working for this cellphone:"+this.getCellPhone().getCellPhoneName());
  }
  public CellPhone getCellPhone() {
    return cellPhone;
  }
  public void setCellPhone(CellPhone cellPhone) {
    this.cellPhone = cellPhone;
    this.getCellPhone().setCellPhoneCPU(this);// 装配(桥接,或者可以认为对象类与其属性类的传递)
  }
  public String getCpuName() {
    return cpuName;
  }
  public void setCpuName(String cpuName) {
    this.cpuName = cpuName;
  }
}

package test.design.bridge;
/**
 * 属性操作系统被抽象成一个维度,为了以后扩展
 * @author lushuaiyin
 *
 */
public abstract class CellPhoneSystem {
  public CellPhone cellPhone;
  public String SystemName;
  public void systemWorks(){
    System.out.println("I am "+this.getSystemName()+" system.");
    System.out.println("I am working for this cellphone:"+this.getCellPhone().getCellPhoneName());
  }
  public CellPhone getCellPhone() {
    return cellPhone;
  }
  public void setCellPhone(CellPhone cellPhone) {
    this.cellPhone = cellPhone;
    this.getCellPhone().setCellPhoneSystem(this);// 装配(桥接,或者可以认为对象类与其属性类的传递)
  }
  public String getSystemName() {
    return SystemName;
  }
  public void setSystemName(String systemName) {
    SystemName = systemName;
  }
}

3. 具体的维度属性对象。

这里我们在操作系统属性和cpu属性上各定义2个具体对象,

package test.design.bridge;
public class AndroidSystem extends CellPhoneSystem{
}

package test.design.bridge;
public class IOSSystem extends CellPhoneSystem{
}

package test.design.bridge;
/**
 * 双核cpu
 * @author Administrator
 *
 */
public class TwoCore extends CellPhoneCPU{
}

package test.design.bridge;
/**
 * 四核cpu
 * @author Administrator
 *
 */
public class FourCore extends CellPhoneCPU{
}

4. 测试代码。

其中说了在需要扩展维度的情况下,怎么扩展的。

定义一个手机对象

package test.design.bridge;
public class Phone1 extends CellPhone{
  //具体对象的属性与逻辑
}

测试main函数

package test.design.bridge;
public class TestMain {
  /**
   * @param args
   */
  public static void main(String[] args) {
    //任何一种具体的对象都是复杂多种属性的集合,在此可以看出桥接模式在构建对象时的灵活性
    //产生一个具体对象1
    CellPhone p1=new Phone1();
    p1.setCellPhoneName(" IPhone 6 ");
    CellPhoneSystem system1=new IOSSystem();//操作系统属性维度
    system1.setSystemName("ios7");
    system1.setCellPhone(p1);//装配
    system1.systemWorks();//工作
    /*装配说的简单点就是传值。因为我们把一个对象的属性按维度分开来了,
     那么桥接的时候就必须相互传递对象。即对象类可以调用子属相类对象,
     子属性类对象也可以调用该对象类.
     关于这样的传值方式有多种,你可以在构造函数中传递,也可以在
    调用具体逻辑方法时传递。这里我直接用set方法传递,只是为了更清楚.
    如果某个属性维度是必须出现的,那就可以在抽象类的构造函数中传入*/
    CellPhoneCPU cpu1=new TwoCore();//cpu属性维度
    cpu1.setCpuName("A6");
    cpu1.setCellPhone(p1);
    cpu1.cpuWorks();
    p1.works();//最终整体对象功能
    /*
    桥接模式就是为了应对属性的扩展,在此说的属性必须是在维度确定的情况下。
    比如,这里我们在定义手机对象时,确定两个属性维度:操作系统和cpu型号。
    以后再这两个属性中,需要扩展时,就可以使用该模式。比如,一种新的cpu
    型号出现了,那么我不用重新设计现在的代码,只要增添一个cpu类即可。
    如果出现了新的维度属性,比如手机对象必须考虑屏幕大小。那桥接模式
    在此就需要从根本上修改代码来了。
    */
    System.out.println("-----------分割---------------------------");
    //在cpu维度上扩展。比如出现新型cpu:8核三星Exynos 5 Octa芯片".
    //三星手机推出了GALAXY Note Ⅲ就是使用这种新型cpu. 写一个新类EightCore扩展cpu维度.
    //同时定义这个手机对象GALAXY Note Ⅲ为PhoneGalaxyNote3
    CellPhone note3=new PhoneGalaxyNote3();
    note3.setCellPhoneName("GALAXY Note Ⅲ");
    CellPhoneSystem system2=new AndroidSystem();
    system2.setSystemName("android4");
    system2.setCellPhone(note3);//装配
    system2.systemWorks();//工作
    CellPhoneCPU cpu2=new EightCore();//最新8核cpu
    cpu2.setCpuName("三星Exynos 5 Octa芯片");
    cpu2.setCellPhone(note3);
    cpu2.cpuWorks();
    note3.works();//三星GALAXY Note Ⅲ新体验
  }
}

如果需要扩展,定义新的维度属性

package test.design.bridge;
public class EightCore extends CellPhoneCPU {
}

package test.design.bridge;
public class PhoneGalaxyNote3 extends CellPhone{
  //具体对象的属性与逻辑
}

测试打印;

I am ios7 system.
I am working for this cellphone: IPhone 6
I am cpu. My pattern is:A6
I am working for this cellphone: IPhone 6
---------------------
This cellphone is: IPhone 6 ,welcome to use.
This cellphone detail infomation:
系统类型:ios7
cpu型号:A6
---------------------
-----------分割---------------------------
I am android4 system.
I am working for this cellphone:GALAXY Note Ⅲ
I am cpu. My pattern is:三星Exynos 5 Octa芯片
I am working for this cellphone:GALAXY Note Ⅲ
---------------------
This cellphone is:GALAXY Note Ⅲ,welcome to use.
This cellphone detail infomation:
系统类型:android4
cpu型号:三星Exynos 5 Octa芯片
---------------------

更多java相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。