面向对象设计模式:结构型模式之适配器模式
创始人
2024-05-30 04:57:03
0

一、引入

在这里插入图片描述

  • Object Oriented Adapters
    在这里插入图片描述
    在这里插入图片描述

在这里插入图片描述

二、XX 模式

aka:Wrapper (包装器)

2.1 Intent 意图

  • Convert the interface of a class into another interface clients expect. 将一个类的接口转换成客户希望的另外一个接口.
    • 作为两个不兼容的接口之间的桥梁
  • 适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作

2.2 Applicability 适用性

  • You want to use an existing class, and its interface does not match the one you need.
  • you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don’t necessarily have compatible interfaces.
  • (object adapter only,仅对象适配器) you need to use several existing subclasses, but it’s impractical to adapt their interface by subclassing every one. 需要使用几个现有的子类,但是通过对每个子类进行子类化来适配它们的接口是不切实际的。
    • An object adapter can adapt the interface of its parent class. 对象适配器可以适配其(所有子类)父类的接口.

2.3 类图

  • 类适配器:多继承(not for java)、多实现
  • 对象适配器:关联,依赖组合

在这里插入图片描述

  • Client: Collaborates with objects conforming to the Target interface. 与符合目标接口的对象进行协作
  • Target: Defines the domain-specific interface that Client uses, It should be an interface. 定义 Client 使用的特定领域的接口,它应该是一个接口
  • Adaptee: Defines an existing interface that needs adapting, could be an interface, or abstract class, or class. 定义需要适配的现有接口,Adaptee 可以是接口、抽象类或类
  • Adapter: Adapts the interface of Adaptee to the Target interface. 适配 Adaptee 的接口到目标接口

2.4 实例:鸭子与火鸡

鸭子与火鸡:
If it walks like a duck and quacks like a duck, then it might be a turkey wrapped with a duck adapter
如果它像鸭子一样走路,那么它可能是一只带着鸭子适配器的火鸡

  • 鸭子
public interface Duck {public void quack();public void fly();
}
public class MallardDuck implements Duck {public void quack() {System.out.println("Quack");}public void fly() {System.out.println("I'm flying");}
}
  • 火鸡
public interface Turkey {public void gobble();public void fly();
}
public class WildTurkey implements Turkey {public void gobble() {System.out.println("Gobble gobble");}public void fly() {System.out.println("I'm flying a short distance");}
}
  • 火鸡适配器:适配火鸡接口
public class TurkeyAdapter implements Duck {Turkey turkey;public TurkeyAdapter(Turkey turkey) {this.turkey = turkey;}public void quack() {turkey.gobble();}public void fly() {for (int i = 0; i < 5; i++) {turkey.fly();}}
}
public class DuckTestDrive {public static void main(String[] args) {MallardDuck duck = new MallardDuck();WildTurkey turkey = new WildTurkey();Duck turkeyAdapter = new TurkeyAdapter(turkey);System.out.println("The Turkey says...");turkey.gobble();turkey.fly();System.out.println("\nThe Duck says...");testDuck(duck);System.out.println("\nThe TurkeyAdapter says...");testDuck(turkeyAdapter);}static void testDuck(Duck duck) {duck.quack();duck.fly();}
}

在这里插入图片描述

  • 鸭子适配器:适配鸭子
public class DuckAdapter implements Turkey {Duck duck;Random rand;public DuckAdapter(Duck duck) {this.duck = duck;rand = new Random();}public void gobble() {duck.quack();}public void fly() {if (rand.nextInt(5) == 0) {duck.fly();}}
}
public class TurkeyTestDrive {public static void main(String[] args) {MallardDuck duck = new MallardDuck();Turkey duckAdapter = new DuckAdapter(duck);for (int i = 0; i < 10; i++) {System.out.println("The DuckAdapter says...");duckAdapter.gobble();duckAdapter.fly();}}
}

在这里插入图片描述


在这里插入图片描述

2.5 实例:适配枚举到迭代器

The early collections types (Vector, Stack, Hashtable, and a few others) implement a method elements(), which returns an Enumeration.

Adapting an Enumeration to an Iterator.

  • Target:Iterator
  • Adaptee:Enumeration
  • Adapter:EnumerationIterator
@SuppressWarnings("unchecked")
public class EnumerationIterator implements Iterator {Enumeration enumeration;public EnumerationIterator(Enumeration enumeration) {this.enumeration = enumeration;}public boolean hasNext() {return enumeration.hasMoreElements();}public Object next() {return enumeration.nextElement();}public void remove() {throw new UnsupportedOperationException();}
}
public class EnumerationIteratorTestDrive {@SuppressWarnings("unchecked")public static void main (String args[]) {// Vector v = new Vector(Arrays.asList(args));Vector v = new Vector(Arrays.asList("A", "B", "C", "E", "F", "G"));Iterator iterator = new EnumerationIterator(v.elements());while (iterator.hasNext()) {System.out.println(iterator.next());}}
}

以迭代器形式遍历枚举。

相关内容

热门资讯

安卓系统目前哪个最好,探索当前... 你有没有想过,手机里的安卓系统就像是一群各具特色的英雄,每个都有自己独特的技能和魅力。那么,问题来了...
安卓系统怎么把图标锁上,图标锁... 手机里的图标乱糟糟的,是不是也想给它们来个“小锁”,让它们乖乖待在原地呢?别急,今天就来教你怎么把安...
微软换账号注册安卓系统,安卓系... 你有没有想过,有一天你的安卓手机突然焕然一新,仿佛重获新生?这可不是普通的升级,而是因为微软的账号注...
怎么停止安卓系统更新,轻松关闭... 手机更新又来了!是不是每次安卓系统更新,你的手机都像被施了魔法,速度变慢,电池续航也跟着缩水?别急,...
股票买平板还是安卓系统,股票投... 最近是不是也被手机屏幕的诱惑给勾住了?想换一台新平板,但又纠结是买苹果的iPad还是安卓系统的平板呢...
iso系统比安卓安全,超越安卓... 你知道吗?在手机操作系统这个江湖里,ISO系统和安卓系统可是两大门派,各有所长。但今天,我要给你揭秘...
安卓手机系统排名图片,揭秘市场... 你有没有发现,现在安卓手机市场上,各种品牌的手机层出不穷,让人眼花缭乱?今天,就让我带你一起走进这个...
安卓系统手机没电图片,电量耗尽... 手机没电了,这可是个让人头疼的小麻烦呢!想象你正沉浸在追剧的乐趣中,突然屏幕一黑,手机没电了。这时候...
安卓系统温控模块在哪里,核心功... 你有没有遇到过手机发热的情况,是不是觉得手机就像个小暖炉,让你有点招架不住呢?别急,今天就来给你揭秘...
最流畅的安卓原生系统,探索安卓... 你有没有想过,为什么你的手机用起来有时候那么卡,有时候又那么流畅呢?这背后,其实隐藏着一个秘密——那...
安卓系统可以刷windows吗... 你有没有想过,你的安卓手机或者平板,竟然能变身成一台Windows电脑?是的,你没听错,安卓系统是可...
苹果安卓操作系统,全面对比与深... 你有没有想过,为什么你的手机里装了那么多应用,而别人的手机却看起来那么清爽?这背后,其实就是苹果的i...
安卓tv中文系统,功能解析与使... 你有没有发现,家里的安卓TV最近变得超级智能呢?没错,就是那个曾经只能看看视频、玩玩游戏的小家伙,现...
谷歌安卓10系统打不开,原因排... 最近是不是你也遇到了这个让人头疼的问题:谷歌安卓10系统打不开?别急,让我来帮你一步步排查,找出原因...
安卓系统转苹果吃鸡,体验全新i... 你知道吗?最近身边的小伙伴们都在热议一个话题:从安卓系统转到苹果手机,体验吃鸡游戏的新鲜感。这可不是...
双系统d盘安装安卓,轻松实现手... 你有没有想过,在电脑上同时拥有Windows系统和安卓系统,是不是就像拥有了两个世界的大门呢?想象一...
安卓系统后台运行程序,高效运行... 你有没有发现,手机里的安卓系统后台运行程序有时候就像那些调皮的小精灵,悄无声息地在你不知道的时候忙碌...
苹果ipad如何改安卓系统,轻... 你有没有想过,把你的苹果iPad换成安卓系统,是不是会有一种全新的使用体验呢?想象那些你熟悉的安卓应...
安卓隐藏系统状态栏,安卓隐藏系... 你有没有发现,手机屏幕下方那个小小的状态栏,有时候真的挺碍眼的?尤其是当你沉浸在游戏或者追剧的时候,...
bb10系统安装安卓,轻松实现... 你有没有想过,把那老式的BB10系统升级成安卓,让它焕发第二春呢?想象你的老手机瞬间变成了一个功能强...