行为型/命令模式/CommandService.cs
(开头部分) 2KB这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。
namespace DesignModeConsole.行为型.命令模式;
/// <summary>
/// 命令模式服务
/// </summary>
public class CommandService : IService
{
private static readonly Queue<IRequestCommand> queue = new Queue<IRequestCommand>();
public void Main()
{
_ = queue.EnsureCapacity(5);
// 后台消费请求
HandlerReqestBackgroupService();
// 前端生产请求
while (true)
{
var requests = new List<IRequestCommand>();
#region 伪造请求
var random = new Random();
for (var i = 0; i < random.Next(1, 5); i++)
{
if (i % 2 == 0)
{
requests.Add(new SendSmsCommand());
}
else
{
requests.Add(new SendEmailCommand());
}
}
#endregion
foreach (var request in requests)
{
IRequestCommand command = null;
if (request is SendSmsCommand sendSmsCommand)
{
command = sendSmsCommand;
}
else if (request is SendEmailCommand sendEmailCommand)
{
command = sendEmailCommand;
}
queue.Enqueue(command);
}
Thread.Sleep(1000);
}
}
private void HandlerReqestBackgroupService()
{
Task.Factory.StartNew(async () =>
后面还有 41 行代码,解锁后查看完整代码
24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明
AI 解读 AI
登录后可用,每次 10 算力,解读结果公开显示在下面。
还没有人解读过这个文件。