日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

Adapter pattern

發(fā)布時(shí)間:2025/4/5 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Adapter pattern 小編覺(jué)得挺不錯(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 {/*** 購(gòu)買游戲道具*/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("您購(gòu)買了 " + count + " 個(gè)道具!");}}/*** 測(cè)試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的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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