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

以迭代器形式遍历枚举。

相关内容

热门资讯

安卓系统小额扣款在哪看 你有没有遇到过这种情况:手机里突然少了点零花钱,却不知道是哪个APP偷偷扣了款?别急,今天就来教你怎...
安卓8系统兼容面具吗 你有没有想过,你的安卓8系统手机能不能装上那些酷炫的面具应用呢?这可是个让人心头一紧的问题,毕竟谁不...
安卓手机系统升级流程,从准备到... 你有没有发现,你的安卓手机最近总是时不时地提醒你系统要升级啦?是不是有点小激动,又有点小紧张呢?别急...
51虚拟机安卓系统,功能解析与... 你有没有想过,在手机上也能体验到电脑的强大功能呢?没错,这就是51虚拟机安卓系统的魅力所在!今天,就...
王者安卓苹果互通系统,安卓苹果... 哇,你知道吗?最近在游戏界可是掀起了一股热潮,那就是《王者荣耀》的安卓和苹果互通系统!是不是听起来就...
黑苹果单系统安卓,黑苹果单系统... 亲爱的读者们,你是否曾梦想过拥有一台既能运行Windows系统,又能畅玩安卓应用的电脑呢?今天,就让...
安卓系统怎么开启隐藏键,安卓系... 你有没有发现安卓手机里藏着一些小秘密呢?比如那些隐藏的键,它们就像手机里的宝藏,等着你去发现和开启。...
华为安卓系统开机页面,科技美学... 亲爱的读者,你是否曾好奇过,当你按下华为手机的开机键,那独特的安卓系统开机页面背后隐藏着怎样的故事?...
安卓系统手机号标记,一键识别号... 你有没有发现,现在用安卓系统手机的人越来越多了呢?手机号标记这个功能,简直就是咱们日常生活中的小帮手...
鸿蒙5.0系统和安卓区别,系统... 你知道吗?最近手机圈子里可是炸开了锅,因为华为的新操作系统鸿蒙5.0系统终于发布了!这可是个大新闻,...
安装安卓系统套件的软件,软件应... 你有没有想过,给你的手机换换口味?是的,就是那种从iOS跳到安卓的感觉,是不是听起来就有点小刺激呢?...
安卓系统各组件怎么安装,安卓系... 你有没有想过,安卓系统里那些神奇的组件是怎么安装到你的手机上的呢?是不是觉得这个过程神秘又复杂?别急...
安卓系统cookie设置阻止,... 你有没有发现,在使用安卓手机的时候,有时候网页会偷偷地跟踪你的浏览记录呢?别担心,今天就来教你怎么设...
2023安卓最流畅系统,性能与... 你有没有发现,手机用久了,就像人一样,开始变得“臃肿”起来?尤其是安卓系统,有时候感觉就像一个老态龙...
安卓子系统的终极玩法,终极玩法... 你有没有发现,安卓系统就像一个巨大的宝藏库,里面藏着无数好玩的游戏和实用的应用?今天,就让我带你一起...
安卓系统主题英文名,探索安卓系... 你有没有发现,手机里的世界其实可以变得五彩斑斓?没错,就是那个我们每天离不开的安卓系统,它就像一个巨...
安卓系统怎么调亮屏幕,安卓系统... 手机屏幕暗暗的,是不是觉得看东西都费劲?别急,今天就来教你怎么轻松调亮安卓系统的屏幕亮度,让你的手机...
手机升级安卓系统包,畅享智能生... 亲爱的手机用户们,你是否也和我一样,对手机升级安卓系统包充满了好奇和期待呢?每次手机系统一更新,我们...
新款cs75安卓系统,新款CS... 你有没有听说?最近汽车界可是炸开了锅,因为一款全新的车型——新款CS75,竟然搭载了安卓系统!这可不...
安卓系统送ios皮肤,体验不一... 你知道吗?最近在手机圈里可是掀起了一股热潮呢!那就是——安卓系统送iOS皮肤。是不是听起来有点不可思...