当前位置: 首页 > news >正文

Unity实现设计模式——适配器模式

Unity实现设计模式——适配器模式

适配器模式又称为变压器模式、包装模式(Wrapper) 将一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。
在一个在役的项目期望在原有接口的基础上拓展,那么适配器模式是最适合的。 即需要使用一个已有或新建的类,但这个类又不符合系统的接口,则可以使用适配器模式。

client:需要使用适配器的对象,不需要关心适配器内部的实现,只对接目标角色。
Target:目标角色,和client直接对接,定义了client需要用到的功能。
Adaptee:需要被进行适配的对象。
Adapter:适配器,负责将源对象转化,给client做适配。

在这里插入图片描述
下面还是使用两个例子来说明适配器模式

(一)使用比较抽象的例子来说明

1.Target

class Target
{public virtual void Request(){Debug.Log("Called Target Request()");}
}

2.Adaptee

class Adaptee
{public void SpecificRequest(){Debug.Log("Called SpecificRequest()");}
}

可以看到目标对象和待适配对象并不匹配
使用适配器来让二者联系起来

3.Adapter

class Adapter : Target
{private Adaptee _adaptee = new Adaptee();public override void Request(){// Possibly do some other work//  and then call SpecificRequest_adaptee.SpecificRequest();}
}

4.测试

public class AdapterStructure : MonoBehaviour
{void Start( ){// Create adapter and place a requestTarget target = new Adapter();target.Request();}
}

可以发现两个无关的类很自然的就联系起来了!

(二)使用具体的例子来说明

现在有敌方攻击者的基类,派生出了敌方坦克,同时有敌方机器人但是机器人的接口和敌方攻击者的接口不同,我们需要进行适配

1. IEnemyAttacker

    public interface IEnemyAttacker{void FireWeapon();void DriveForward();void AssignDriver(string driver);}

2. EnemyTank

    public class EnemyTank : IEnemyAttacker{public void FireWeapon(){int attackDamage = Random.Range(1, 10);Debug.Log("Enemy Tank does " + attackDamage + " damage");}public void DriveForward(){int movement = Random.Range(1, 5);Debug.Log("Enemy Tank moves " + movement + " spaces");}public void AssignDriver(string driver){Debug.Log(driver + " is driving the tank");}}

3. EnemyRobot

    public class EnemyRobot{public void SmashWithHands(){int attackDamage = Random.Range(1, 10);Debug.Log("Robot causes " + attackDamage + " damage with it hands");}public void WalkForward(){int movement = Random.Range(1, 3);Debug.Log("Robot walks " + movement + " spaces");}public void ReactToHuman(string driverName){Debug.Log("Robot tramps on " + driverName);}}

可以看出这里不同,当然可以对EnemyRobot派生自IEnemyAttacker接口,然后重新实现接口,但是在多人协作的场景,这样是不允许的。同时该类可能在别处引用,显然有很大的工作量要修改。

4. EnemyRobotAdaper

    public class EnemyRobotAdaper : IEnemyAttacker{EnemyRobot robot;public EnemyRobotAdaper(EnemyRobot robot){this.robot = robot;}public void FireWeapon(){robot.SmashWithHands();}public void DriveForward(){robot.WalkForward();}public void AssignDriver(string driver){robot.ReactToHuman(driver);}}

5.测试

    public class AdapterPatternExample2 : MonoBehaviour{void Start(){IEnemyAttacker tank = new EnemyTank();EnemyRobot fredTheRobot = new EnemyRobot();IEnemyAttacker adapter = new EnemyRobotAdaper(fredTheRobot);fredTheRobot.ReactToHuman("Hans");fredTheRobot.WalkForward();tank.AssignDriver("Frank");tank.DriveForward();tank.FireWeapon();adapter.AssignDriver("Mark");adapter.DriveForward();adapter.FireWeapon();}}
http://www.lryc.cn/news/191352.html

相关文章:

  • 【2023年11月第四版教材】专题1 - 计算题考点汇总 (合集篇)
  • 系统架构设计:17 论信息系统的安全性与保密性设计
  • 使用EasyDarwin+ffmpeg+EasyPlayerPro完成rtsp的推流操作和拉流操作
  • FPGA project : flash_secter_erase
  • HarmonyOS/OpenHarmony原生应用-ArkTS万能卡片组件Radio
  • python opencv 深度学习 指纹识别算法实现 计算机竞赛
  • 一图看懂CodeArts Inspector 三大特性,带你玩转漏洞管理服务
  • 【Mysql】Mysql的启动选项和系统变量(二)
  • FPGA project : flash_read
  • nnunetv2训练报错 ValueError: mmap length is greater than file size
  • React知识点系列(2)-每天10个小知识
  • AutoGPT:让 AI 帮你完成任务事情 | 开源日报 No.54
  • USB 转串口芯片 CH340
  • Day 05 python学习笔记
  • Python如何17行代码画一个爱心
  • 生产环境中常用Linux命令
  • 【使用 TensorFlow 2】03/3 创建自定义损失函数
  • Vue3中使用v-model高级用法参数绑定传值
  • 你的工作中,chatGPT可以帮你做什么?
  • k8s简单部署nginx
  • 小黑子—MyBatis:第四章
  • Docker快速上手:使用Docker部署Drupal并实现公网访问
  • React知识点系列(1)-每天10个小知识
  • substring 和 substr 的区别
  • 产品经理的工作职责是什么?
  • 智能井盖传感器:提升城市安全与便利的利器
  • 给你一个项目,你将如何开展性能测试工作?
  • 点燃市场热情,让产品风靡全球——实用推广策略大揭秘!
  • Python操作Hive数据仓库
  • 客户收到报价后突然安静了,怎么办?