设计模式(18)桥接模式

一、介绍:

1、定义:桥接(Bridge)模式属于结构型设计模式。通过提供抽象化和实现化之间的桥接结构,来实现二者的解耦。把抽象(abstraction)与行为实现(implementation)分离开来,从而可以保持各部分的独立性以及应对它们的功能扩展。简而言之,实现系统可能有多角度分类,每一种分类都有可能变化,那么就把这种多角度分离出来让他们独立变化,减少他们之间的耦合。

2、组成结构:UML结构图如下:

(1) Abstraction(抽象类):它是用于定义抽象类的接口,通常是抽象类而不是接口,其中定义了Implementor(实现类接口)类型的对象并可以维护该对象,它与Implementor具有关联关系,它既可以包含抽象业务方法,也可以包含具体业务方法。

复制代码
public abstract class Abstraction{
  private Implementor impl;//定义实现类接口对象
  public void setImpl(Implementor impl){
      this.impl = impl;
  }
  public abstract void operation();//声明抽象业务方法
}

(2) RefinedAbstraction(扩充抽象类):它扩充由Abstraction定义的接口,通常情况下它不再是抽象类而是具体类,实现了在Abstraction中的抽象业务方法,并且可以调用在Implementor中定义的业务方法。

复制代码
public class RefinedAbstraction extends Abstraction{
  public void operation(){
    //业务代码
    impl.operationImpl();//调用实现类的方法
    //业务代码
  }
}

(3) Implementor(实现类接口):它是定义实现类的接口,不一定要与Abstraction的接口完全一致,事实上这两个接口可以完全不同。一般而言,Implementor接口仅提供基本操作,而Abstraction定义的接口可能会做更多更复杂的操作。Implementor对这些基本操作进行了声明,而具体实现交给其子类。通过关联关系,在Abstraction中不仅拥有自己的方法,还可以调用Implementor中定义的方法,使用关联关系替代继承关系。

复制代码
public interface Implementor{
   public void operationImpl();
}

(4) ConcreteImplementor(具体实现类):它具体实现了Implementor接口,在不同的ConcreteImplementor中提供基本操作的不同实现,在程序运行时ConcreteImplementor对象将替换父类对象,提供给抽象类具体的业务操作方法。

复制代码
public class ConcreteImplementor implements  Implementor{
   public void operationImpl(){
     //具体业务方法的实现
   }
}

对于客户端而言,可以针对两个维度的抽象层编程,在程序运行时再动态确定两个维度的子类,动态组合对象,将两个独立变化的维度完全解耦,以便能够灵活地扩充任一维度而对另一维度不造成任何影响。

二、demo:

1、颜色画板:

复制代码
//画笔
interface Printer {
   public void print(int radius, int x, int y);
}
class ColorPrinter implements Printer {
   @Override
   public void print(int radius, int x, int y) {
      System.out.println("Color: " + radius +", x: " +x+", "+ y +"]");
   }
}
class BlackPrinter implements Printer {
   @Override
   public void print(int radius, int x, int y) {
      System.out.println("Black: " + radius +", x: " +x+", "+ y +"]");
   }
}

//形状
abstract class Shape {
   protected Printer print;
   protected Shape(Printer p){
      this.print = p;
   }
   public abstract void draw();  
}
class Circle extends Shape {
   private int x, y, radius;

   public Circle(int x, int y, int radius, Printer draw) {
      super(draw);
      this.x = x;  
      this.y = y;  
      this.radius = radius;
   }

   public void draw() {
      print.print(radius,x,y);
   }
}
public class Main {
   public static void main(String[] args) {
      Shape redCircle = new Circle(100,100, 10, new ColorPrinter());
      Shape blackCircle = new Circle(100,100, 10, new BlackPrinter());

      redCircle.draw();
      blackCircle.draw();
   }
}
相关推荐
高晶6 小时前
无乘法器实现LC二阶滤波器:基于移位运算的快速算法与四种精度推导
算法·设计模式·程序员
高晶6 小时前
主从自动分配算法:平均值乘以设备数一半的选举基准值解析
算法·设计模式·程序员
延晗7 小时前
【从零开始 DeepSeek Harness 插件开发教程】二、一切皆插件:简单理解一下 Cordis 内核与 DSH 心智模型
设计模式·deepseek
高晶8 小时前
Android 通讯录应用开发实战:从零搭建 SQLite 数据持久化完整项目
android·设计模式·前端框架
日和雫13 小时前
封装、继承、多态 & 组合,为什么组合大于继承?
设计模式
代钦塔拉16 小时前
【C++/Qt 进阶】设计模式系列(6):桥接模式——把“两个独立变化的维度“拆开
c++·qt·设计模式
haluhalu.16 小时前
深入理解AIAgent(1.3)--- 设计模式
人工智能·python·设计模式·langchain
高晶2 天前
CANopen 在 BMS 系统中的 C 语言实战指南
设计模式·程序员
博风2 天前
13、设计模式应用举例:工厂方法模式:创建药品对象
设计模式·工厂方法模式
代钦塔拉2 天前
【C++/Qt 进阶】设计模式系列(5):适配器模式——给“不兼容的接口“贴一层翻译膜
c++·qt·设计模式