平時我們會經(jīng)常碰到這樣的情況,有了兩個現(xiàn)成的類,它們之間沒有什么聯(lián)系,但是我們現(xiàn)在既想用其中一個類的方法,同時也想用另外一個類的方法。有一個解決方法是,修改它們各自的接口,但是這是我們最不愿意看到的。這個時候Adapter模式就會派上用場了。
Adapter模式也叫適配器模式,是由GoF提出的23種設計模式的一種。Adapter模式是構造型模式之一,通過Adapter模式,可以改變已有類(或外部類)的接口形式。
適配器 模式 有三種方式,一種是對象適配器,一種是類適配器, 一種是接口適配器
以下舉例說明:
1.類適配器
類圖
1
2
3
4
5
6
7
8
9
|
public class DrawRectangle { //畫方 public void drawRectangle(String msg) { System.out.println( "draw Rectangle: " + msg); } } public interface IDrawCircle { //畫圓的接口 void drawCircle(); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/** * 類適配器 使用對象繼承的方式,是靜態(tài)的定義方式 * @author stone * */ public class DrawAdapter4Class extends DrawRectangle implements IDrawCircle { //既能畫方又能畫圓 @Override public void drawCircle() { System.out.println( "DrawAdapter4Class: drawCircle" ); } } |
2.對象適配器
類圖:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
/** * 對象適配器: 使用對象組合的方式,是動態(tài)組合的方式。 * 既能畫方又能畫圓 * @author stone * DrawAdapter是適配器,DrawRectangle屬于adapter,是被適配者,適配器將被適配者和適配目標(DrawCircle)進行適配 * */ public class DrawAdapter4Object implements IDrawCircle { //既能畫方又能畫圓 private DrawRectangle drawRectangle; public DrawAdapter4Object(DrawRectangle drawRectangle) { this .drawRectangle = drawRectangle; } @Override public void drawCircle() { System.out.println( "DrawAdapter4Object: drawcircle" ); } public void drawRectangle(String msg) { drawRectangle.drawRectangle(msg); } } |
3.接口適配器
類圖
1
2
3
4
5
6
7
8
|
/* * 接口適配器:接口中有抽象方法,我們只想實現(xiàn)其中的幾個,不想全部實現(xiàn), * 所以提供一個默認空實現(xiàn),然后繼承自它,重寫實現(xiàn)我們想實現(xiàn)的方法 */ public interface IDraw { void drawCircle(); void drawRectangle(); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
/* * 接口適配器 的默認實現(xiàn) */ public class DefaultDrawAdapter implements IDraw { //畫方 畫圓 皆為空實現(xiàn) @Override public void drawCircle() { } @Override public void drawRectangle() { } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
public class Test { public static void main(String[] args) { //對象適配器 DrawAdapter4Object objAdapter = new DrawAdapter4Object( new DrawRectangle()); objAdapter.drawCircle(); objAdapter.drawRectangle( " in DrawAdapter4Object" ); System.out.println( "--------------" ); //類適配器 DrawAdapter4Class clzAdapter = new DrawAdapter4Class(); clzAdapter.drawCircle(); clzAdapter.drawRectangle( "in DrawAdapter4Class" ); System.out.println( "--------------" ); //接口適配器 MyDrawAdapter myDrawAdapter = new MyDrawAdapter(); myDrawAdapter.drawCircle(); myDrawAdapter.drawRectangle(); } static class MyDrawAdapter extends DefaultDrawAdapter { @Override public void drawCircle() { System.out.println( "drawCircle in MyDrawAdapter" ); } } } |
打印
1
2
3
4
5
6
7
|
DrawAdapter4Object: drawcircle draw Rectangle: in DrawAdapter4Object -------------- DrawAdapter4Class: drawCircle draw Rectangle: in DrawAdapter4Class -------------- drawCircle in MyDrawAdapter |
適配器模式的三個特點:
1 適配器對象實現(xiàn)原有接口
2 適配器對象組合一個實現(xiàn)新接口的對象(這個對象也可以不實現(xiàn)一個接口,只是一個單純的對象)
3 對適配器原有接口方法的調(diào)用被委托給新接口的實例的特定方法
有人認為講解設計模式的例子都太簡單,看著感覺是那么回事,但是要是真想在項目開發(fā)中使用,還真是應用不到。其實我們不必在項目中刻意使用設計模式,而是應該從實際的設計問題出發(fā),看哪個模式能解決我們的問題,就使用哪個模式。不要為了使用模式而使用模式,那樣就舍本逐末了,一般情況下,只要遵循一定的設計原則就可以了,設計模式也是根據(jù)這些原則被總結出來的,熟悉了這些原則,模式自然而然就有了。