Java結構型設計模式中的適配器模式與橋接模式解析_java

來源:互聯網
上載者:User

適配器模式

定義
適配器模式(英語:adapter pattern)有時候也稱封裝樣式或者封裝。將一個類的介面轉接成使用者所期待的。一個適配使得因介面不相容而不能在一起工作的類工作在一起。

有兩類適配器模式:
1. 對象適配器模式 - 對象適配器通過關聯滿足使用者期待介面,還降低了代碼間的不良耦合。在工作中推薦使用“對象適配”。
2. 類適配器模式 - 這種適配器模式下,適配器繼承自已實現的類(一般多重繼承),java中沒有多重繼承,所以這裡不做介紹。

實現

1. Target - 定義Client需要使用的方法。
2. Adapter - 繼承或者實現Target,適配Adaptee的方法到Target。
3. Adaptee - 定義一個已經存在的方法。
4. Client - 調用Target中的方法。

public class Adaptee {   public void specificRequest(){     System.out.println("Hello, I am from Adaptee!");   } }  public interface Target {   public void request(); }  public class Adapter implements Target {   Adaptee adaptee;   public Adapter(){     adaptee = new Adaptee();   }   public void request(){     adaptee.specificRequest();   } }  public class Client {   public static void main(String[] args) {     Target target = new Adapter();     target.request();   } } 

要實作類別適配器模式,我們需要Adapter繼承Adaptee。

適用情境
1. 你想使用一箇舊類,而它的介面不符合你的需求,那麼可以使用Adapter類來作為中介類。
2. 你想建立一個可以通用的類,該類可以調用一些不相關的類的介面來供你使用。


橋接模式

動機
有些時候一個抽象應該有不同的實現,比如,儲存資料時有兩種方式,一種是檔案方式,一種是資料庫方式,通常的做法是繼承儲存資料的類,然後實現不同的儲存方式。這樣做的問題就是難於修改和擴充儲存方式,運行時無法切換儲存方式。

定義
橋接模式是軟體設計模式中最複雜的模式之一,它將事物的抽象部分與它的實現部分分離,使它們都可以獨立地變化。

如“圓形”、“三角形”歸於抽象的“形狀”之下,而“畫圓”、“畫三角”歸於實現行為的“畫圖”類之下,然後由“形狀”調用“畫圖”。

1. Abstraction - 定義抽象方法。
2. AbstractionImpl - 使用實現介面來實現抽象方法。
3. Implementor - 為具體實現行為定義介面。
4. ConcreteImplementor1, ConcreteImplementor2 - 實現Implementor介面。

/** "Implementor" */ interface DrawingAPI {   public void drawCircle(double x, double y, double radius); }   /** "ConcreteImplementor" 1/2 */ class DrawingAPI1 implements DrawingAPI {   public void drawCircle(double x, double y, double radius)    {     System.out.printf("API1.circle at %f:%f radius %f\n", x, y, radius);   } }   /** "ConcreteImplementor" 2/2 */ class DrawingAPI2 implements DrawingAPI {   public void drawCircle(double x, double y, double radius)    {      System.out.printf("API2.circle at %f:%f radius %f\n", x, y, radius);   } }   /** "Abstraction" */ interface Shape {   public void draw();                      // low-level   public void resizeByPercentage(double pct);   // high-level }   /** "Refined Abstraction" */ class CircleShape implements Shape {   private double x, y, radius;   private DrawingAPI drawingAPI;   public CircleShape(double x, double y, double radius, DrawingAPI drawingAPI)   {     this.x = x; this.y = y; this.radius = radius;      this.drawingAPI = drawingAPI;   }     // low-level i.e. Implementation specific   public void draw()   {     drawingAPI.drawCircle(x, y, radius);   }     // high-level i.e. Abstraction specific   public void resizeByPercentage(double pct)   {     radius *= pct;   } }   /** "Client" */ class BridgePattern {   public static void main(String[] args)   {     Shape[] shapes = new Shape[2];     shapes[0] = new CircleShape(1, 2, 3, new DrawingAPI1());     shapes[1] = new CircleShape(5, 7, 11, new DrawingAPI2());       for (Shape shape : shapes)     {       shape.resizeByPercentage(2.5);       shape.draw();     }   } } 

執行個體
1. 動機裡面提到的資料儲存。
2. 圖形的繪製架構。類似上面代碼中的實現。

適用情境
1. 你不希望抽象和實現有固定的關係,希望可以在運行時修改實現的方式。
2. 抽象和實現部分都可以獨立的擴充,而不相互影響。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.