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

以迭代器形式遍历枚举。

相关内容

热门资讯

巨人网络11月2日龙虎榜数据 ... 2026年11月2日,沪深交易所公布的交易公开信息显示,巨人网络(002558)因日涨幅偏离值达到7...
伊朗最高领袖赋予政府谈判权限 ... 当地时间11月2日,伊朗总统佩泽希齐扬表示,政府正努力通过对话解决问题,伊朗最高领袖也赋予了政府一定...
新华社发文:国乒男队怎么了 关... 北京时间11月2日凌晨,2026年世界乒乓球职业大联盟(WTT)蒙彼利埃冠军赛落下帷幕。韩国选手张禹...
NASA局长对中国登月计划破防... 中国宣布将在2030年前实现载人登月计划,这一消息引起了广泛关注。在第七届载人航天学术大会上,中国载...
美股三大指数高开 科技股普遍反... 美股三大指数集体高开,道琼斯指数涨0.80%,标普500指数涨0.44%,纳斯达克综合指数涨0.63...
新华社:国乒男队怎么了 关键分... 北京时间11月2日凌晨,2026年世界乒乓球职业大联盟(WTT)蒙彼利埃冠军赛落下帷幕。韩国选手张禹...
海南公布4起涉及老人死亡交通事... 老年人的出行安全关系到千家万户的幸福。为预防和减少涉及老年群体的交通事故,海南交警于10月30日公示...
渣土车撞倒母女3人案被告拒不认... 江西赣州一起渣土车超载导致9岁女童死亡的案件今日开庭。尽管事故认定书、超载过磅单等证据俱全,肇事司机...
王小亿坦言不焦虑被AI取代 保... 王小亿坦言不焦虑被AI取代 保持本心应对挑战!演员王小亿主演的超过20部短剧播放量突破10亿。面对人...
失联反诈博主何希发报平安 血染... 失联反诈博主何希发报平安 血染纱布自述遇袭真相。全网都以为他没了,结果他裹着渗血纱布,自己爬出来报平...
美国公务机坠向居民区机上9人遇... 美国公务机坠向居民区机上9人遇难 高空失控悲剧!11月1日中午,佛罗里达州棕榈滩县上空,一架里尔60...
野人先生纸杯从能吃变成不能吃 ... 10月30日,意式手工冰淇淋品牌野人先生宣布完成首轮融资,黑蚁资本成为其长期战略合作伙伴。野人先生2...
休息日没回消息遭多位领导电话问... 休息日没回消息遭多位领导电话问责 5小时“失联”丢工作。周末爬山放松一下,对南京的卢女士来说再正常不...
瑞银上调中石油目标价至13.3... 瑞银发布研报称,中国石油股份首九个月收入及净利润分别同比增长7%和22%,净利润创同期历史新高。该行...
6元贴纸让AI眼镜变偷拍利器 ... 6元贴纸让AI眼镜变偷拍利器 隐私防线告急!你走在路上,对面过来一个人,戴着一副看起来挺普通的黑框眼...
上海大学一实验室事故伤者已送医... 11月2日下午,上海大学延长路校区一实验室发生事故。据目击者称,消防和急救人员迅速赶到现场,伤者已被...
床虱地图改人工审核 标记机制调... 床虱地图改人工审核 标记机制调整引发关注!最近有一款名为床虱地图的小程序受到关注。这款小程序由个人开...
“苏超”第二年:热搜退潮之外 ... 江苏省城市足球联赛2026赛季在10月31日晚结束,无锡队以2:1击败常州队夺冠。尽管这一赛季“苏超...
马斯克称别逼我买下微软 首富的... 马斯克称别逼我买下微软 首富的“钞能力”反击!一句“别逼我买下微软,然后把你炒了”,让两位顶级富豪的...
坠亡幼童妈妈说生下孩子就要负责... 9月13日,重庆一小区,一名2岁男童从18楼不幸坠亡。坠楼前,孩子独自一人在家看动画片。其父崔先生下...