学习:java设计模式—Adapter模式
生活随笔
收集整理的這篇文章主要介紹了
学习:java设计模式—Adapter模式
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1、核心意圖:
將一個(gè)類的接口轉(zhuǎn)換成客戶希望的另外一個(gè)接口,從而使得原本由于接口不兼容而不能一起工作的類可以一起工作。
該模式的目標(biāo)是通過(guò)一個(gè)代理(這里是Adapter),在原來(lái)的類(Adaptee)和客戶(Client)之間進(jìn)行協(xié)調(diào),從而達(dá)到兼容的目的。其核心是解決一致性的問(wèn)題。
2、身邊實(shí)例:
在我們實(shí)際生活中也很容易看到這方面的例子,比如我們要和一個(gè)外國(guó)人打交道,例如韓國(guó) 人,如果我們沒(méi)有學(xué)習(xí)過(guò)韓語(yǔ),這個(gè)韓國(guó)人也沒(méi)有學(xué)習(xí)過(guò)我們漢語(yǔ),在這種情況下,我們之間是很難進(jìn)行直接交流溝通。為了達(dá)到溝通的目的有兩個(gè)方法:1)改造 這個(gè)韓國(guó)人,使其能夠用漢語(yǔ)進(jìn)行溝通;2)請(qǐng)一個(gè)翻譯,在我們和這個(gè)韓國(guó)人之間進(jìn)行語(yǔ)言的協(xié)調(diào)。顯然第一種方式——改造這個(gè)韓國(guó)人的代價(jià)要高一些,我們不 僅要重新培訓(xùn)他漢語(yǔ)的學(xué)習(xí),還有時(shí)間、態(tài)度等等因素。而第二個(gè)方式——請(qǐng)一個(gè)翻譯,就很好實(shí)現(xiàn),而且成本低,還比較靈活,當(dāng)我們想換個(gè)日本人,再換個(gè)翻譯 就可以了。
3、動(dòng)機(jī)簡(jiǎn)述:
在該模式的動(dòng)機(jī)中,描述了一個(gè)繪圖編輯器的實(shí)現(xiàn),該編輯器可以對(duì)基本圖元Shape(直 線、多邊形、正文等)進(jìn)行繪制和排列來(lái)生成圖片和圖表,對(duì)于這些圖元類的實(shí)現(xiàn),直線/多邊形還比較容易實(shí)現(xiàn),但是正文的實(shí)現(xiàn)卻很麻煩,為了減少開(kāi)發(fā)成本和 保證質(zhì)量,通過(guò)采用Adapter模式定義適配器類TextShape,來(lái)重用圖形工具箱中已經(jīng)存在的正文編輯器TextView。
4、Java實(shí)現(xiàn)分析:
在GOF設(shè)計(jì)模式中,Adapter可以分為類模式和對(duì)象模式兩種,類模式通過(guò)多重繼承實(shí)現(xiàn),對(duì)象模式通過(guò)委托實(shí)現(xiàn)。
在Java中由于沒(méi)有多重繼承機(jī)制,所以要想實(shí)現(xiàn)類模式的Adapter,就要進(jìn)行相應(yīng) 的改變:通過(guò)繼承Adaptee類實(shí)現(xiàn)Target接口方式實(shí)現(xiàn)。這種改變存在兩個(gè)問(wèn)題:1)Target必須是一個(gè)接口而不能是一個(gè)類,否則Adapter無(wú)法implements實(shí)現(xiàn);2)Adapter是繼承Adaptee的實(shí)現(xiàn),而不是私有繼承,這就表示Adapter是一個(gè)Adaptee的子類。
類Adapter模式和對(duì)象Adapter模式的Java代碼可參考本文下方代碼部分。
5、類模式/對(duì)象模式分析:
由于存在兩種Adapter實(shí)現(xiàn)方式(即使在Java中),那么在實(shí)際中我們采用哪一種要好呢?通過(guò)分析發(fā)現(xiàn)這兩種模式有兩個(gè)主要特性區(qū)別,并且還是互補(bǔ)的:
A、表現(xiàn)在Adapter對(duì)Adaptee的特殊性要求:
類模式由于Adapter是Adaptee的子類,所以Adapter很方便重新定義Adaptee中的個(gè)別方法,以達(dá)到自己的特性需要。
對(duì)象模式由于Adapter不是Adaptee的子類,所以如果Adapter對(duì)Adaptee中的個(gè)別方法有特殊的需要,就要新建Adaptee的子類,而讓Adapter使用這個(gè)子類。
B、表現(xiàn)在Adaptee的類層次擴(kuò)展上:
類模式由于Adapter是Adaptee的子類,所以編譯后就不能再更換所實(shí)現(xiàn)的父類Adaptee,因此如果有一個(gè)Adaptee的類層次結(jié)構(gòu),就要相應(yīng)的有一個(gè)Adapter的類層次結(jié)構(gòu),且新擴(kuò)展Adaptee時(shí)很不方便。
對(duì)象模式由于Adapter不是Adaptee的子類,而是通過(guò)使用的方式,所以在系統(tǒng)運(yùn)行時(shí)仍然可以更換Adapter所使用的Adaptee,只要他們具有相同的類型。所以在新擴(kuò)展Adaptee時(shí)很方便。
6、Java代碼示例—對(duì)象模式實(shí)現(xiàn):
類Point,表示畫(huà)面坐標(biāo)中的點(diǎn)
package qinysong.pattern.adapter;
public class Point {
? private int coordinateX;
? private int coordinateY;
? public Point(int coordinateX, int coordinateY){
??? this.coordinateX = coordinateX;
??? this.coordinateY = coordinateY;
? }
? public String toString(){
??? return "Point[x=" + coordinateX + ",y=" + coordinateY + "]";
? }
? public int getCoordinateX() {
??? return coordinateX;
? }
? public int getCoordinateY() {
??? return coordinateY;
? }
}
類Shape,表示圖元借口,對(duì)應(yīng)Adapter模式中的Target package qinysong.pattern.adapter;
public interface Shape {
? public Point getBottomLeftPoint();
? public Point getTopRightPoint();
}
類TextView,工具箱中的文本組件類,已經(jīng)存在的類,對(duì)應(yīng)Adapter模式中的Adaptee package qinysong.pattern.adapter;
public class TextView {
? public int getCoordinateX() {
??? System.out.println("TextView.getCoordinateX()...");
??? return 10;
? }
? public int getCoordinateY() {
??? System.out.println("TextView.getCoordinateY()...");
??? return 20;
? }
? public int getHeight() {
??? System.out.println("TextView.getHeight()...");
??? return 30;
? }
? public int getWidth() {
??? System.out.println("TextView.getWidth()...");
??? return 40;
? }
? public boolean isEmpty(){
??? return false;
? }
}
類TextShape,對(duì)象模式實(shí)現(xiàn)的Adapter package qinysong.pattern.adapter;
public class TextShape implements Shape {
? private TextView textView;
? public TextShape(TextView textView){
??? this.textView = textView;
? }
? //通過(guò)TextView的實(shí)例進(jìn)行協(xié)調(diào)實(shí)現(xiàn)
? public Point getBottomLeftPoint() {
??? System.out.println("TextShape.getBottomLeftPoint()...");
??? int coordinateX = textView.getCoordinateX();
??? int coordinateY = textView.getCoordinateY();
??? return new Point(coordinateX, coordinateY);
? }
? //通過(guò)TextView的實(shí)例進(jìn)行協(xié)調(diào)實(shí)現(xiàn)
? public Point getTopRightPoint() {
??? System.out.println("TextShape.getTopRightPoint()...");
??? int coordinateX = textView.getCoordinateX();
??? int coordinateY = textView.getCoordinateY();
??? int height = textView.getHeight();
??? int width = textView.getWidth();
??? return new Point(coordinateX + width, coordinateY + height);
? }
}
類Client,Adapter模式的客戶 package qinysong.pattern.adapter;
public class Client {
? public static void main(String[] args){
??? System.out.println("Client.main begin ..........");
??? System.out.println("Client.main 以下是通過(guò)實(shí)例委托方式實(shí)現(xiàn)的Adapter");
??? Shape shape = new TextShape(new TextView());
??? Point bottomLeft = shape.getBottomLeftPoint();
??? Point topRight = shape.getTopRightPoint();
??? System.out.println("Client.main shape's bottomLeft:" + bottomLeft);
??? System.out.println("Client.main shape's topRight:" + topRight);
??? System.out.println(" Client.main 以下是通過(guò)類繼承方式實(shí)現(xiàn)的Adapter");
??? Shape shape2 = new TextShape2();
??? bottomLeft = shape2.getBottomLeftPoint();
??? topRight = shape2.getTopRightPoint();
??? System.out.println("Client.main shape2's bottomLeft:" + bottomLeft);
??? System.out.println("Client.main shape2's topRight:" + topRight);
??? System.out.println("Client.main end?? ..........");
? }
}
7、Java代碼示例—類模式實(shí)現(xiàn): 和以上對(duì)象模式實(shí)現(xiàn)中的示例目的相同,類Point、Shape、TextView相同,略。以下是類TextShape2的示例代碼,實(shí)現(xiàn)類模式的Adapter package qinysong.pattern.adapter;
public class TextShape2 extends TextView implements Shape {
? //通過(guò)所繼承的TextView,進(jìn)行協(xié)調(diào)實(shí)現(xiàn)
? public Point getBottomLeftPoint() {
??? System.out.println("TextShape2.getBottomLeftPoint()...");
??? int coordinateX = getCoordinateX();
??? int coordinateY = getCoordinateY();
??? return new Point(coordinateX, coordinateY);
? }
? //通過(guò)所繼承的TextView,進(jìn)行協(xié)調(diào)實(shí)現(xiàn)
? public Point getTopRightPoint() {
??? System.out.println("TextShape2.getTopRightPoint()...");
??? int coordinateX = getCoordinateX();
??? int coordinateY = getCoordinateY();
??? int height = getHeight();
??? int width = getWidth();
??? return new Point(coordinateX + width, coordinateY + height);
? }
? //注意: 這一點(diǎn)體現(xiàn)了類模式的優(yōu)勢(shì),可以很方便地重定義父類TextView中的方法
? public int getCoordinateX() {
??? System.out.println("TextShape2.getCoordinateX()...");
??? return 100;
? }
}
public class Point {
? private int coordinateX;
? private int coordinateY;
? public Point(int coordinateX, int coordinateY){
??? this.coordinateX = coordinateX;
??? this.coordinateY = coordinateY;
? }
? public String toString(){
??? return "Point[x=" + coordinateX + ",y=" + coordinateY + "]";
? }
? public int getCoordinateX() {
??? return coordinateX;
? }
? public int getCoordinateY() {
??? return coordinateY;
? }
}
類Shape,表示圖元借口,對(duì)應(yīng)Adapter模式中的Target package qinysong.pattern.adapter;
public interface Shape {
? public Point getBottomLeftPoint();
? public Point getTopRightPoint();
}
類TextView,工具箱中的文本組件類,已經(jīng)存在的類,對(duì)應(yīng)Adapter模式中的Adaptee package qinysong.pattern.adapter;
public class TextView {
? public int getCoordinateX() {
??? System.out.println("TextView.getCoordinateX()...");
??? return 10;
? }
? public int getCoordinateY() {
??? System.out.println("TextView.getCoordinateY()...");
??? return 20;
? }
? public int getHeight() {
??? System.out.println("TextView.getHeight()...");
??? return 30;
? }
? public int getWidth() {
??? System.out.println("TextView.getWidth()...");
??? return 40;
? }
? public boolean isEmpty(){
??? return false;
? }
}
類TextShape,對(duì)象模式實(shí)現(xiàn)的Adapter package qinysong.pattern.adapter;
public class TextShape implements Shape {
? private TextView textView;
? public TextShape(TextView textView){
??? this.textView = textView;
? }
? //通過(guò)TextView的實(shí)例進(jìn)行協(xié)調(diào)實(shí)現(xiàn)
? public Point getBottomLeftPoint() {
??? System.out.println("TextShape.getBottomLeftPoint()...");
??? int coordinateX = textView.getCoordinateX();
??? int coordinateY = textView.getCoordinateY();
??? return new Point(coordinateX, coordinateY);
? }
? //通過(guò)TextView的實(shí)例進(jìn)行協(xié)調(diào)實(shí)現(xiàn)
? public Point getTopRightPoint() {
??? System.out.println("TextShape.getTopRightPoint()...");
??? int coordinateX = textView.getCoordinateX();
??? int coordinateY = textView.getCoordinateY();
??? int height = textView.getHeight();
??? int width = textView.getWidth();
??? return new Point(coordinateX + width, coordinateY + height);
? }
}
類Client,Adapter模式的客戶 package qinysong.pattern.adapter;
public class Client {
? public static void main(String[] args){
??? System.out.println("Client.main begin ..........");
??? System.out.println("Client.main 以下是通過(guò)實(shí)例委托方式實(shí)現(xiàn)的Adapter");
??? Shape shape = new TextShape(new TextView());
??? Point bottomLeft = shape.getBottomLeftPoint();
??? Point topRight = shape.getTopRightPoint();
??? System.out.println("Client.main shape's bottomLeft:" + bottomLeft);
??? System.out.println("Client.main shape's topRight:" + topRight);
??? System.out.println(" Client.main 以下是通過(guò)類繼承方式實(shí)現(xiàn)的Adapter");
??? Shape shape2 = new TextShape2();
??? bottomLeft = shape2.getBottomLeftPoint();
??? topRight = shape2.getTopRightPoint();
??? System.out.println("Client.main shape2's bottomLeft:" + bottomLeft);
??? System.out.println("Client.main shape2's topRight:" + topRight);
??? System.out.println("Client.main end?? ..........");
? }
}
7、Java代碼示例—類模式實(shí)現(xiàn): 和以上對(duì)象模式實(shí)現(xiàn)中的示例目的相同,類Point、Shape、TextView相同,略。以下是類TextShape2的示例代碼,實(shí)現(xiàn)類模式的Adapter package qinysong.pattern.adapter;
public class TextShape2 extends TextView implements Shape {
? //通過(guò)所繼承的TextView,進(jìn)行協(xié)調(diào)實(shí)現(xiàn)
? public Point getBottomLeftPoint() {
??? System.out.println("TextShape2.getBottomLeftPoint()...");
??? int coordinateX = getCoordinateX();
??? int coordinateY = getCoordinateY();
??? return new Point(coordinateX, coordinateY);
? }
? //通過(guò)所繼承的TextView,進(jìn)行協(xié)調(diào)實(shí)現(xiàn)
? public Point getTopRightPoint() {
??? System.out.println("TextShape2.getTopRightPoint()...");
??? int coordinateX = getCoordinateX();
??? int coordinateY = getCoordinateY();
??? int height = getHeight();
??? int width = getWidth();
??? return new Point(coordinateX + width, coordinateY + height);
? }
? //注意: 這一點(diǎn)體現(xiàn)了類模式的優(yōu)勢(shì),可以很方便地重定義父類TextView中的方法
? public int getCoordinateX() {
??? System.out.println("TextShape2.getCoordinateX()...");
??? return 100;
? }
}
總結(jié)
以上是生活随笔為你收集整理的学习:java设计模式—Adapter模式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 快捷方式全部变成LNK文件修复方法
- 下一篇: Tox —— 保证通话信息安全的即时聊天