设计模式(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();
   }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w_t_y_y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值