日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Adapter pattern

發(fā)布時(shí)間:2025/4/5 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Adapter pattern 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. 定義?http://en.wikipedia.org/wiki/Adapter_pattern

An adapter helps two incompatible interfaces to work together. This is the real world definition for an adapter. The adapter design pattern is used when you want two different classes with incompatible interfaces to work together. Interfaces may be incompatible but the inner functionality should suit the need. The Adapter pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients.

2. 分類?http://en.wikipedia.org/wiki/Adapter_pattern

There are two types of adapter patterns:

Object Adapter pattern

In this type of adapter pattern, the adapter contains an instance of the class it wraps. In this situation, the adapter makes calls to the instance of the wrapped?object.

Class Adapter pattern

This type of adapter uses multiple?polymorphic interfaces?to achieve its goal. The adapter is created by implementing or inheriting both the interface that is expected and the interface that is pre-existing. It is typical for the expected interface to be created as a pure?interface?class, especially in?languages?such as?Java?that do not support?multiple inheritance.

??

?

3. 實(shí)例?http://blog.chinaunix.net/uid-29140694-id-4138579.html

? ?1. 類適配器

public interface Target {/*** 源角色也包含的方法*/public void operation1();/*** 原角色不包含的方法*/public void operation2(); }/*** 原角色實(shí)現(xiàn)類*/ public class Adaptee {public void operation1(){System.out.println("operation-1");} }/*** 適配器角色*/ public class Adapter extends Adaptee implements Target{@Overridepublic void operation2() {System.out.println("operation-2");} }

? 2. 對(duì)象適配器

/*** 抽象目標(biāo)接口*/ public interface Target {/*** 購買游戲道具*/public void buyGameProps(); } /*** 被適配對(duì)象 */ public class Rmb {/*** 金額屬性*/public int money;/*** 構(gòu)造方法* @param money*/public Rmb(int money){this.money = money;}public int getMoney() {return money;}public void setMoney(int money) {this.money = money;}} /*** QQ幣實(shí)現(xiàn)類(適配器角色)*/ public class QQCoin implements Target{/*** 人民幣實(shí)體對(duì)象*/public Rmb rmbImpl;/*** QQ幣數(shù)量*/public int count;/*** 構(gòu)造方法* @param rmb*/public QQCoin(Rmb rmb){this.count = rmb.getMoney() * 10;}/*** 具體實(shí)現(xiàn)方法*/@Overridepublic void buyGameProps() {System.out.println("您購買了 " + count + " 個(gè)道具!");}}/*** 測試Main方法*/ public class TestMain {public static void main(String [] args){Rmb rmb = new Rmb(5);QQCoin coin = new QQCoin(rmb);coin.buyGameProps();} }

對(duì)象適配器的優(yōu)點(diǎn):
(1)適配器模式可以理解成是在原有基礎(chǔ)上的封裝,不需要對(duì)原有程序進(jìn)行改動(dòng),即可實(shí)現(xiàn)特定功能。
(2)對(duì)象適配器可以服務(wù)于多個(gè)源角色,便于程序的擴(kuò)展。

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/davidwang456/p/3844925.html

總結(jié)

以上是生活随笔為你收集整理的Adapter pattern的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。