RequestQueue 执行队列

自己的一个知识点:一个程序设计方法。定义个队列,按顺序执行。
using System;
using System.Collections.Generic;

public enum RequestQueueState
{
    Idle = 0,
    Busy = 1,
}

public interface IRequestCommand
{
    void ExecCommand();
}

public class RequestQueue
{
    protected List<IRequestCommand> m_requests;

    public RequestQueue()
    {
        m_requests = new List<IRequestCommand>();
    }

    public virtual void AddRequest(IRequestCommand command)
    {
        m_requests.Add(command);
    }

    public virtual void InsertRequest(int pos, IRequestCommand command)
    {
        m_requests.Insert(pos, command);
    }

    public virtual int RequestCount()
    {
        return m_requests.Count;
    }

    public virtual void DoNextRequest()
    {
        if (m_requests.Count <= 0)
        {
            return;
        }

        IRequestCommand requestCommand = m_requests[0];
        requestCommand.ExecCommand();
        m_requests.RemoveAt(0);
    }

    public virtual void ReDoNextRequest()
    {

    }

    public virtual void Clear()
    {
        m_requests.Clear();
    }

    public virtual bool IsEmpty()
    {
        return m_requests.Count == 0;
    }
}

public class RequestQueueN : RequestQueue
{
    protected bool m_needRomve;
    protected int m_curIndex = 0;

    public RequestQueueN(bool needRomve = false) : base()
    {
        m_needRomve = needRomve;
        m_curIndex = 0;
    }

    public override void AddRequest(IRequestCommand command)
    {
        base.AddRequest(command);
    }

    public override void InsertRequest(int pos, IRequestCommand command)
    {
        m_requests.Insert(pos, command);
    }

    public override int RequestCount()
    {
        return m_requests.Count;
    }

  	public override void DoNextRequest()
    {
        if (m_requests.Count <= 0)
        {
            return;
        }

        if (m_needRomve)
        {
            base.DoNextRequest();
        }
        else
        {
            if (m_curIndex < m_requests.Count)
            {
                IRequestCommand requestCommand = m_requests[m_curIndex];
                m_curIndex++;
                requestCommand.ExecCommand();
            }
        }
    }

    public override void ReDoNextRequest()
    {
        if (m_needRomve)
        {
            base.ReDoNextRequest();
        }
        else
        {
            m_curIndex = 0;
            DoNextRequest();
        }
    }

  	public override void Clear()
    {
        base.Clear();
        m_curIndex = 0; 
    }

    public override bool IsEmpty()
    {
        if (m_needRomve)
        {
            return base.IsEmpty();
        }
        else
        {
            return m_curIndex >= m_requests.Count;
        }
    }
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值