游戏设计模式-命令模式

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
 * Author:W
 * 命令模式:当发送一个指令、或命令、或执行请求时,如果是立即执行的,则只需调用功能提供者的方法执行即可。
 * 但是,如果需要对这些请求命令进行管理,即可以新增、撤销、排序、调度、延迟执行等操作时,则需要将这些请求“对象化”,
 * 并创建命令管理者来统一进行管理
 * 
 */
namespace WCommand
{


    #region  执行操作及参数封装-》命令对象化
    /// <summary>
    /// 命令的基类
    /// </summary>
    public abstract class Command
    {
        public abstract void Excute();
    }

    /// <summary>
    /// 命令1
    /// 执行操作及参数封装-》对象化
    /// </summary>
    public class Command1 : Command
    {
        private Receiver1 receiver1 = null;
        private string param = "";

        public Command1(Receiver1 receiver1,string param)
        {
            this.receiver1 = receiver1;
            this.param = param;
        }

        public override void Excute()
        {
            this.receiver1.Action(this.param); 
        }
    }
    #endregion


    #region 命令对应的“执行操作”的封装【注意:根据实际情况,可以不用再封装这些执行操作,直接在具体Command类中Excute中调用需要执行的方法】
    /// <summary>
    /// 功能执行者1
    /// </summary>
    public class Receiver1
    {
        public void Action(string param)
        {
            
        }
    }

    #endregion


    /// <summary>
    /// 命令管理者
    /// </summary>
    public class CommandManager
    {
        private List<Command> commands = new List<Command>();

        /// <summary>
        /// 新增新的命令
        /// </summary>
        /// <param name="command"></param>
        public void AddCommand(Command command)
        {
            commands.Add(command); 
        }

        /// <summary>
        /// 移除最后一个命令
        /// </summary>
        public void RemoveCommand()
        {
            if (commands.Count == 0)
                return;

            commands.RemoveAt(commands.Count-1);
        }


        /// <summary>
        /// 命令执行
        /// </summary>
        public void ExcuteCommand()
        {
            //---------------如何执行的一个规则,根据项目实际情况来定-----------
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Data菌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值