//抽象接受者,用来处理命令的 public abstract class Receive { public abstract void Do(); }
//抽象命令 public interface ICommand { void execude(); }
//具体命令 public class ACommand : ICommand { private Receive mReceive; public ACommand(Receive r) { mReceive = r; } public void execude() { mReceive.Do(); } }
//具体接受者 using UnityEngine; public class AReceive : Receive { public override void Do() { Debug.Log("A Command execude"); } }
//调用者,负责调用命令的 public class Invoker { private ICommand command; public void SetCommand(ICommand c) { command = c; } public void action() { command.execude(); } }
//测试使用 using UnityEngine; public class testCommand : MonoBehaviour { void Start () { AReceive aReceive = new AReceive(); ICommand command = new ACommand(aReceive); Invoker invoker = new Invoker(); invoker.SetCommand(command); invoker.action(); } }
主要分为3种角色
Command类型就是一个命令,本省不包含处理的方法,用于识别。
Receiver接受者,就是接受到命令后,处理的。
Invoker调用者,就是命令的触发者。