Java結(jié)構(gòu)型設(shè)計(jì)模式中的適配器模式與橋接模式解析
適配器模式
定義
適配器模式(英語:adapter pattern)有時(shí)候也稱包裝樣式或者包裝。將一個(gè)類的接口轉(zhuǎn)接成用戶所期待的。一個(gè)適配使得因接口不兼容而不能在一起工作的類工作在一起。
有兩類適配器模式:
1. 對象適配器模式 - 對象適配器通過關(guān)聯(lián)滿足用戶期待接口,還降低了代碼間的不良耦合。在工作中推薦使用“對象適配”。
2. 類適配器模式 - 這種適配器模式下,適配器繼承自已實(shí)現(xiàn)的類(一般多重繼承),java中沒有多重繼承,所以這里不做介紹。
實(shí)現(xiàn)
1. Target - 定義Client需要使用的方法。
2. Adapter - 繼承或者實(shí)現(xiàn)Target,適配Adaptee的方法到Target。
3. Adaptee - 定義一個(gè)已經(jīng)存在的方法。
4. Client - 調(diào)用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(); } }
要實(shí)現(xiàn)類適配器模式,我們需要Adapter繼承Adaptee。
適用場景
1. 你想使用一個(gè)舊類,而它的接口不符合你的需求,那么可以使用Adapter類來作為中介類。
2. 你想創(chuàng)建一個(gè)可以通用的類,該類可以調(diào)用一些不相關(guān)的類的接口來供你使用。
橋接模式
動機(jī)
有些時(shí)候一個(gè)抽象應(yīng)該有不同的實(shí)現(xiàn),比如,保存數(shù)據(jù)時(shí)有兩種方式,一種是文件方式,一種是數(shù)據(jù)庫方式,通常的做法是繼承保存數(shù)據(jù)的類,然后實(shí)現(xiàn)不同的保存方式。這樣做的問題就是難于修改和擴(kuò)展保存方式,運(yùn)行時(shí)無法切換保存方式。
定義
橋接模式是軟件設(shè)計(jì)模式中最復(fù)雜的模式之一,它將事物的抽象部分與它的實(shí)現(xiàn)部分分離,使它們都可以獨(dú)立地變化。
如“圓形”、“三角形”歸于抽象的“形狀”之下,而“畫圓”、“畫三角”歸于實(shí)現(xiàn)行為的“畫圖”類之下,然后由“形狀”調(diào)用“畫圖”。
1. Abstraction - 定義抽象方法。
2. AbstractionImpl - 使用實(shí)現(xiàn)接口來實(shí)現(xiàn)抽象方法。
3. Implementor - 為具體實(shí)現(xiàn)行為定義接口。
4. ConcreteImplementor1, ConcreteImplementor2 - 實(shí)現(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(); } } }
實(shí)例
1. 動機(jī)里面提到的數(shù)據(jù)保存。
2. 圖形的繪制框架。類似上面代碼中的實(shí)現(xiàn)。
適用場景
1. 你不希望抽象和實(shí)現(xiàn)有固定的關(guān)系,希望可以在運(yùn)行時(shí)修改實(shí)現(xiàn)的方式。
2. 抽象和實(shí)現(xiàn)部分都可以獨(dú)立的擴(kuò)展,而不相互影響。
相關(guān)文章
透徹理解Java中Synchronized(對象鎖)和Static Synchronized(類鎖)的區(qū)別
這篇文章主要介紹了Java中Synchronized(對象鎖)和Static Synchronized(類鎖)的區(qū)別,希望對大家有所幫助,一起跟隨小編過來看看吧2018-05-05Java SimpleDateFormat中英文時(shí)間格式化轉(zhuǎn)換詳解
這篇文章主要為大家詳細(xì)介紹了Java SimpleDateFormat中英文時(shí)間格式化轉(zhuǎn)換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12詳解eclipse將項(xiàng)目打包成jar文件的兩種方法及問題解決方法
本文給大家介紹了eclipse中將項(xiàng)目打包成jar文件的兩種方法及其遇到問題解決方法,本文圖文并茂給大家介紹的非常詳細(xì),需要的朋友可以參考下2017-12-12