命令模式
命令模式是具现化的方法调用,是一种回调的面向对象实现。
例子
玩家自定义配置按键的功能
如下为常见的简单实现
if(Input.GetKeyDown(KeyCode.Space))
Debug.Log("Jump");
else if(Input.GetKeyDown(KeyCode.LeftArrow))
Debug.Log("Left");
else if(Input.GetKeyDown(KeyCode.RightArrow))
Debug.Log("Right");
首先定义基类保存不同功能所绑定的按键
public class InputCommandBase
{
public KeyCode curBindingInputKeycode;
public virtual void Execute()
{
//按键对应操作
}
}
定义不同子类实现不同的按键功能
public class InputJump : InputCommandBase
{
public InputJump(KeyCode bindingInputKeyCode)
{
curBindingInputKeycode = bindingInputKeyCode;
}
public override void Execute()
{
base.Execute