当前位置: 首页 > 工具软件 > RequestQueue > 使用案例 >

RequestQueue 执行队列

陆星文
2023-12-01
自己的一个知识点:一个程序设计方法。定义个队列,按顺序执行。
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;
        }
    }
}

 

 

 

 类似资料: