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

以迭代器形式遍历枚举。

相关内容

热门资讯

安卓系统相机不能启动,安卓相机... 手机里的安卓系统相机突然不能启动了,这可真是让人头疼啊!你有没有遇到过这种情况呢?别急,今天就来跟你...
安卓原生系统时间校准,基于安卓... 手机时间不准了?别急,我来教你如何轻松搞定安卓原生系统时间校准! 话题引入:手机时间不准,是不是让你...
主机系统内存和安卓联机,主机系... 你有没有想过,为什么你的手机在玩大型游戏时总是卡得要命?又或者,为什么你的电脑在处理复杂任务时,反应...
安卓如何手机上刷系统,轻松升级... 你有没有想过,你的安卓手机是不是已经有点儿“老态龙钟”了呢?别急,别急,今天就来教你怎么给它来个“青...
苹果系统观战安卓好友,观战新体... 亲爱的读者,你是否也有过这样的经历:一边享受着苹果系统的优雅与流畅,一边又忍不住好奇地观战安卓好友们...
安卓系统最好是哪个,最佳生成方... 你有没有想过,手机里的安卓系统哪个才是最适合你的呢?在这个信息爆炸的时代,手机已经成为了我们生活中不...
改时间安卓系统vivo,探索v... 你有没有发现,最近你的vivo手机有点儿“慢吞吞”的?别急,别急,让我来给你支个招儿,让你的安卓系统...
安卓系统的旋钮在哪,旋钮生成位... 你有没有发现,有时候手机上的小细节也能让人头疼不已?比如说,安卓系统的旋钮在哪?这问题看似简单,但不...
安卓手机app系统软件,探索安... 你有没有发现,现在手机里的app简直就像是个小宇宙,各种功能应有尽有,让人眼花缭乱。尤其是安卓手机,...
win111安卓子系统,开启跨... 哇,你有没有听说最近的大新闻?那就是Windows 11的安卓子系统!是的,你没听错,Windows...
游戏摇杆连安卓系统电视,畅享游... 你有没有想过,家里的安卓系统电视也能玩起游戏来?没错,就是那种让你手舞足蹈、热血沸腾的游戏摇杆!今天...
nokia平板系统兼容安卓,尽... 你有没有想过,那些曾经陪伴我们度过无数时光的诺基亚手机,现在竟然也能摇身一变,成为平板电脑的得力助手...
安卓原生系统是什么品牌,探索安... 你有没有想过,为什么你的手机那么流畅,界面那么美观?这背后,可是有一个强大的“大脑”在默默支撑着呢!...
安卓3大操作系统,从三大分支看... 你知道吗?在安卓的世界里,操作系统可是有着三大巨头呢!它们就像安卓世界的三驾马车,各自有着独特的魅力...
开源文件管理系统安卓,打造个性... 你有没有想过,手机里那些乱糟糟的文件,要是能有个好帮手,生活该有多轻松啊?今天,就让我带你走进一个神...
手机删除了系统安卓市场,手机系... 手机里的安卓市场突然不见了,这可怎么办呢?别急,让我来给你详细说说这个棘手的问题,让你轻松应对!一、...
安卓系统写脚本软件下载,基于安... 你有没有想过,你的安卓手机或者平板电脑,除了用来刷剧、玩游戏,还能变成一个强大的工作助手呢?没错,就...
安卓系统有哪些机型好,探索顶级... 你有没有想过,安卓系统里的手机型号那么多,哪一款才是最适合你的呢?别急,今天我就来给你好好盘点看看安...
安卓系统之间如何互传,安卓设备... 你是不是也和我一样,手机里存了那么多好东西,却苦于不能和好友分享呢?别急,今天就来教你怎么用安卓系统...
安卓系统启动修改工具,安卓系统... 你有没有想过,你的安卓手机启动速度竟然可以像火箭一样快?没错,这就是今天我要跟你分享的神秘工具——安...