适配器模式
2017-03-20 10:21:03 0 举报
适配器模式是一种结构型设计模式,它结合了两个不兼容的接口。通过将一个类的接口转换为客户端期望的另一个接口,使得原本因接口不兼容而不能一起工作的那些类可以一起工作。适配器模式分为类适配器和对象适配器两种类型,前者使用多重继承关系进行适配,后者使用组合关系来实现适配。适配器模式主要用于在运行时根据需要对接口进行转换,以解决由于接口不兼容而无法一起工作的问题。
作者其他创作
大纲/内容
Guards
attack(){fc.attack1()}
这里的适配器使得外籍中锋可以参与团队比赛适配器模式可以使不适用某场景的对象适用此场景ForeigeCenter不会attack只会attack1,而修改此类(让这个外籍人学英语)又不方便(其他地方可能调用到attack1),只有引入适配器
defense(){fc.defense1()}
defense(){}
Adaptor
attack(){}
abstract defence()
abstract Player
Player p
defense1(){}
ForreigeCenter(外籍中锋,听不懂英文,需要适配器)
abstract attack()
Adaptor(Player p){this.p = p}
Forwards
Player b = new Forwards()b.attack()Player m = new Guards()m.attack()Player c = new Adaptor(new ForeigeCenter())c.attack()c.defense()
attack1(){}
0 条评论
下一页
为你推荐
查看更多