設計模式之橋接模式(結構型)

@[toc]

模式定義

橋接模式(Bridge Pattern)是將抽象部分和實現部分分離,使它們可以獨立地改變,是一種對象結構型模式。

模式角色

橋接模式包含如下角色:

  • Abstraction(抽象類)
  • RefinedAbstraction(擴充抽象類)
  • Implementor(實現類接口)
  • ConcreteImplementor(具體實現類)

模式分析

橋接模式關鍵在于如何將抽象化與實現化解耦,使得兩者可以獨立改變。

抽象化:抽象就是忽略一些信息,將不同的實體當作同樣的實體對待。在面向對象中將對象的共同性質抽取出來形成類的過程稱之為抽象化的過程

實現化:針對抽象話給出的具體實現,就是實現化,抽象化與實現化是互逆的過程

解耦:解耦就是將抽象化和實現化直接的耦合解脫開,或者說將兩者之間的強關聯變成弱關聯,將兩個角色由繼承改成關聯關系(組合或者聚合)

典型代碼:

public interface Implementor
{
    public void operationImpl();
} 

public abstract class Abstraction
{
    protected Implementor impl;
    
    public void setImpl(Implementor impl)
    {
        this.impl=impl;
    }
    
    public abstract void operation();
} 

public class RefinedAbstraction extends Abstraction
{
    public void operation()
    {
        //代碼
        impl.operationImpl();
        //代碼
    }
} 

模式例子

畫出不同顏色的圓,DrawAPI 接口的實體類 RedCircle、GreenCircle。Shape 是一個抽象類,例子來自:http://www.runoob.com/design-pattern/bridge-pattern.html

創(chuàng)建橋接接口:

public interface DrawAPI {
   public void drawCircle(int radius, int x, int y);
}

接口具體實現類:

public class RedCircle implements DrawAPI {
   @Override
   public void drawCircle(int radius, int x, int y) {
      System.out.println("Drawing Circle[ color: red, radius: "
         + radius +", x: " +x+", "+ y +"]");
   }
}
public class GreenCircle implements DrawAPI {
   @Override
   public void drawCircle(int radius, int x, int y) {
      System.out.println("Drawing Circle[ color: green, radius: "
         + radius +", x: " +x+", "+ y +"]");
   }
}

抽象類關聯方式實現接口:

public abstract class Shape {
   protected DrawAPI drawAPI;
   protected Shape(DrawAPI drawAPI){
      this.drawAPI = drawAPI;
   }
   public abstract void draw();  
}

具體類實現抽象類:

public class Circle extends Shape {
   private int x, y, radius;
 
   public Circle(int x, int y, int radius, DrawAPI drawAPI) {
      super(drawAPI);
      this.x = x;  
      this.y = y;  
      this.radius = radius;
   }
 
   public void draw() {
      drawAPI.drawCircle(radius,x,y);
   }
}

public class BridgePatternDemo {
   public static void main(String[] args) {
      Shape redCircle = new Circle(100,100, 10, new RedCircle());
      Shape greenCircle = new Circle(100,100, 10, new GreenCircle());
 
      redCircle.draw();
      greenCircle.draw();
   }
}

打印到控制臺:

Drawing Circle[ color: red, radius: 10, x: 100, 100]
Drawing Circle[  color: green, radius: 10, x: 100, 100]

模式應用

  • 一些軟件的跨平臺設計有時候也是應用了橋接模式
  • JDBC的驅動程序,實現了將不同類型的數據庫與Java程序的綁定
  • Java虛擬機實現了平臺的無關性,Java虛擬機設計就是通過橋接模式
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 【學習難度:★★★☆☆,使用頻率:★★★☆☆】直接出處:橋接模式梳理和學習:https://github.com/...
    BruceOuyang閱讀 1,066評論 0 2
  • 1.初識橋接模式 將抽象部分與它的實現部分分離,使它們都可以獨立地變化。 Abstraction:抽象部分的接口。...
    王偵閱讀 1,017評論 0 7
  • 創(chuàng)建型模式 工廠模式 工廠模式(Factory Pattern)是 Java 中最常用的設計模式之一。這種類型的設...
    隔墻送來秋千影閱讀 2,819評論 0 11
  • 一、應用場景 設想如果要繪制矩形、圓形、橢圓、正方形,我們至少需要4個形狀類,但是如果繪制的圖形需要具有不同的顏色...
    QuantRuu閱讀 829評論 0 51
  • 創(chuàng)建型模式 工廠模式 工廠模式(Factory Pattern)是 Java 中最常用的設計模式之一。這種類型的設...
    liuyang7519閱讀 391評論 0 2

友情鏈接更多精彩內容