做网站用什么语言,cn域名知名网站,南京江北新区房价走势最新消息,个人网站做cpaUnity实现设计模式——适配器模式
适配器模式又称为变压器模式、包装模式#xff08;Wrapper#xff09; 将一个类的接口变换成客户端所期待的另一种接口#xff0c;从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。 在一个在役的项目期望在原有接口的基础…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();}}