面向对象设计模式:结构型模式之适配器模式
创始人
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());}}
}

以迭代器形式遍历枚举。

相关内容

热门资讯

如何删除安卓系统信息,安卓系统... 手机里的信息越来越多,是不是感觉自己的隐私都快要被暴露无遗了?别担心,今天就来教你怎么轻松删除安卓系...
国产安卓系统哪个流畅,揭秘哪家... 你有没有想过,手机里的操作系统就像是我们的大脑,有时候它运行得快慢,直接影响我们的使用体验呢?今天,...
安卓怎么刷主机系统,掌握主机系... 你有没有想过,你的安卓手机或者平板,是不是也能像电脑一样,装上各种酷炫的系统呢?没错,今天就要来给你...
安卓系统桌面显示时间,见证日常... 你有没有发现,每次打开安卓手机,那桌面上的时间总是一闪一闪的,仿佛在告诉你:“时间不等人哦!”今天,...
os云刷安卓系统,畅享智能新体... 你有没有想过,你的安卓手机是不是也能像电脑一样,装上各种酷炫的系统呢?没错,今天就要来跟你聊聊这个神...
安卓系统如何装siri,安卓设... 你有没有想过,在安卓手机上也能享受到Siri的智能语音助手服务呢?没错,就是那个在iPhone上呼之...
电脑怎么安卓双系统,电脑安装安... 你有没有想过,一台电脑同时运行安卓和Windows系统,那得多酷啊!想象一边处理工作,一边刷刷抖音,...
linux操作系统与安卓系统,... 你有没有想过,为什么你的手机可以随时随地给你发送信息,而你的电脑却能帮你处理复杂的任务呢?这背后,就...
安卓大屏系统信息,功能与体验全... 你有没有发现,最近手机界的大屏风潮真是势不可挡啊!安卓大屏系统信息,这可是个热门话题呢!想象拿着一块...
苹果7转安卓系统,探索安卓系统... 你有没有想过,从苹果7转到安卓系统会是怎样的体验呢?想象你手中的那个曾经陪伴你度过无数美好时光的苹果...
安卓系统打卡手机推荐,高效便捷... 你有没有想过,每天早上起床后,第一件事就是拿出手机打卡签到?这已经成为现代生活中不可或缺的一部分了。...
安卓手机装了小米系统,小米系统... 你有没有想过,把安卓手机的灵魂换成小米的系统,会是怎样的体验呢?想象原本流畅的安卓系统,突然间被注入...
安卓汽车系统怎么升级,轻松实现... 亲爱的安卓车主们,你是否也像我一样,对汽车系统升级充满了好奇和期待呢?想象你的爱车就像一部智能手机,...
手机怎么变安卓系统,轻松实现系... 你有没有想过,你的手机居然可以变身成为安卓系统呢?没错,就是那个自由度极高的操作系统!今天,就让我来...
安卓仿ios系统横条,打造流畅... 你有没有发现,最近安卓手机上出现了一种特别的新花样——仿iOS系统的横条设计!这可不是简单的模仿,而...
谷歌汽车安卓系统下载,引领智能... 你有没有想过,未来的汽车可能会变成一个移动的智能中心?想象你的汽车不仅能够带你穿梭在城市的大街小巷,...
安卓系统硬件修复软件,全面解析... 手机里的安卓系统突然卡顿,是不是让你头疼不已?别急,今天就来给你揭秘那些神奇的安卓系统硬件修复软件,...
鸿蒙系统比安卓系统大么,体积对... 你有没有想过,手机里的操作系统,就像是手机的心脏,它的大小、性能,都直接影响到手机的使用体验。今天,...
安卓系统 刷机模式,刷机模式操... 你有没有想过,你的安卓手机其实就像一个隐藏着无限可能的宝藏呢?没错,今天就要来跟你聊聊这个宝藏的秘密...
案卷制作系统下载安卓,高效便捷... 你有没有想过,在手机上也能轻松制作案卷呢?没错,现在就有这么一款神器——案卷制作系统,而且它还支持安...