<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 橋接模式 橋接(Bridge)是用于把抽象化與實現化解耦,使得二者可以獨立變化。這種類型的設計模式屬于結構型模式,它通過提供抽象化和實現化之間的橋接結構,來實現二者的解耦。 這種模式涉及到一個作為橋接的接口,使得實體類的功能獨立于接口實現類。這兩種類型的類可被結構化改變而互不影響。 我們通過下面的實例來演示橋接模式(Bridge Pattern)的用法。其中,可以使用相同的抽象類方法但是不同的橋接實現類,來畫出不同顏色的圓。 ## 介紹 **意圖:**將抽象部分與實現部分分離,使它們都可以獨立的變化。 **主要解決:**在有多種可能會變化的情況下,用繼承會造成類爆炸問題,擴展起來不靈活。 **何時使用:**實現系統可能有多個角度分類,每一種角度都可能變化。 **如何解決:**把這種多角度分類分離出來,讓它們獨立變化,減少它們之間耦合。 **關鍵代碼:**抽象類依賴實現類。 **應用實例:** 1、豬八戒從天蓬元帥轉世投胎到豬,轉世投胎的機制將塵世劃分為兩個等級,即:靈魂和肉體,前者相當于抽象化,后者相當于實現化。生靈通過功能的委派,調用肉體對象的功能,使得生靈可以動態地選擇。 2、墻上的開關,可以看到的開關是抽象的,不用管里面具體怎么實現的。 **優點:** 1、抽象和實現的分離。 2、優秀的擴展能力。 3、實現細節對客戶透明。 **缺點:**橋接模式的引入會增加系統的理解與設計難度,由于聚合關聯關系建立在抽象層,要求開發者針對抽象進行設計與編程。 **使用場景:** 1、如果一個系統需要在構件的抽象化角色和具體化角色之間增加更多的靈活性,避免在兩個層次之間建立靜態的繼承聯系,通過橋接模式可以使它們在抽象層建立一個關聯關系。 2、對于那些不希望使用繼承或因為多層次繼承導致系統類的個數急劇增加的系統,橋接模式尤為適用。 3、一個類存在兩個獨立變化的維度,且這兩個維度都需要進行擴展。 **注意事項:**對于兩個獨立變化的維度,使用橋接模式再適合不過了。 ## 實現 我們有一個作為橋接實現的 _DrawAPI_ 接口和實現了 _DrawAPI_ 接口的實體類 _RedCircle_、_GreenCircle_。_Shape_ 是一個抽象類,將使用 _DrawAPI_ 的對象。_BridgePatternDemo_,我們的演示類使用 _Shape_ 類來畫出不同顏色的圓。 ![橋接模式的 UML 圖](https://box.kancloud.cn/2015-12-13_566cdfb59891e.jpg) ### 步驟 1 創建橋接實現接口。 _DrawAPI.java_ ``` public interface DrawAPI { public void drawCircle(int radius, int x, int y); } ``` ### 步驟 2 創建實現了 _DrawAPI_ 接口的實體橋接實現類。 _RedCircle.java_ ``` 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 +"]"); } } ``` _GreenCircle.java_ ``` 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 +"]"); } } ``` ### 步驟 3 使用 _DrawAPI_ 接口創建抽象類 _Shape_。 _Shape.java_ ``` public abstract class Shape { protected DrawAPI drawAPI; protected Shape(DrawAPI drawAPI){ this.drawAPI = drawAPI; } public abstract void draw(); } ``` ### 步驟 4 創建實現了 _Shape_ 接口的實體類。 _Circle.java_ ``` 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); } } ``` ### 步驟 5 使用 _Shape_ 和 _DrawAPI_ 類畫出不同顏色的圓。 _BridgePatternDemo.java_ ``` 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(); } } ``` ### 步驟 6 驗證輸出。 ``` Drawing Circle[ color: red, radius: 10, x: 100, 100] Drawing Circle[ color: green, radius: 10, x: 100, 100] ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看