国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

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

2019-11-26 14:38:16
字體:
來源:轉載
供稿:網友

適配器模式

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

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

實現(xiàn)

20162384930660.png (475×274)

1. Target - 定義Client需要使用的方法。
2. Adapter - 繼承或者實現(xiàn)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();   } } 

要實現(xiàn)類適配器模式,我們需要Adapter繼承Adaptee。

適用場景
1. 你想使用一個舊類,而它的接口不符合你的需求,那么可以使用Adapter類來作為中介類。
2. 你想創(chuàng)建一個可以通用的類,該類可以調用一些不相關的類的接口來供你使用。


橋接模式

動機
有些時候一個抽象應該有不同的實現(xiàn),比如,保存數據時有兩種方式,一種是文件方式,一種是數據庫方式,通常的做法是繼承保存數據的類,然后實現(xiàn)不同的保存方式。這樣做的問題就是難于修改和擴展保存方式,運行時無法切換保存方式。

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

如“圓形”、“三角形”歸于抽象的“形狀”之下,而“畫圓”、“畫三角”歸于實現(xiàn)行為的“畫圖”類之下,然后由“形狀”調用“畫圖”。

20162384958680.png (629×299)

1. Abstraction - 定義抽象方法。
2. AbstractionImpl - 使用實現(xiàn)接口來實現(xiàn)抽象方法。
3. Implementor - 為具體實現(xiàn)行為定義接口。
4. ConcreteImplementor1, ConcreteImplementor2 - 實現(xiàn)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. 圖形的繪制框架。類似上面代碼中的實現(xiàn)。

適用場景
1. 你不希望抽象和實現(xiàn)有固定的關系,希望可以在運行時修改實現(xiàn)的方式。
2. 抽象和實現(xiàn)部分都可以獨立的擴展,而不相互影響。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 连江县| 融水| 古浪县| 深水埗区| 宁都县| 浮梁县| 元氏县| 外汇| 丰城市| 城市| 射阳县| 柯坪县| 岱山县| 宜兴市| 临沭县| 镇平县| 蕉岭县| 遂溪县| 古交市| 丰台区| 化德县| 即墨市| 和政县| 定襄县| 龙陵县| 黎川县| 开鲁县| 屯门区| 武隆县| 和林格尔县| 双江| 胶州市| 黎平县| 松江区| 巨野县| 新乡县| 德昌县| 德庆县| 仙游县| 黄龙县| 延津县|