首页 > 学院 > 开发设计 > 正文

设计模式-命令

2019-11-06 06:19:15
字体:
来源:转载
供稿:网友
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using 设计模式讲解.Command;namespace 设计模式讲解{    class PRogram    {        static void Main(string[] args)        {            /*命令模式,场景使用不同的方式实现多线程编程             命令:用户发送不同的命令,接收者执行对应的方法             接收者:负责接收命令并且执行命令             调用者:负责调用命令            */            List<int> caculateList = new List<int>();            for (int i = 0; i < 10; i++)            {                caculateList.Add(i);            }            MTReceiver _MTReceiver = new MTReceiver();            ThreadPoolCmd _ThreadPoolCmd = new ThreadPoolCmd(_MTReceiver);            ParallelCmd _ParallelCmd = new ParallelCmd(_MTReceiver);            TaskCmd _TaskCmd = new TaskCmd(_MTReceiver);            Invoker _Invoker = new Invoker();            _Invoker.SetCommand(_ParallelCmd);            _Invoker.ExcuteCommand(caculateList);            Console.ReadKey();        }    }}

MTReceiver

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Threading;namespace 设计模式讲解.Command{    public class MTReceiver    {        /// <summary>        /// 线程池        /// </summary>        /// <param name="caculateList"></param>        public void ThreadPoolMethod(List<int> caculateList)        {            foreach (var item in caculateList)            {                ThreadPool.QueueUserWorkItem(new WaitCallback(t =>                {                    Console.WriteLine("线程池计算:{0},线程ID:{1}", t, Thread.CurrentThread.ManagedThreadId);                }),item);            }        }        /// <summary>        /// 并行        /// </summary>        /// <param name="caculateList"></param>        public void ParallelMethod(List<int> caculateList)        {            Parallel.ForEach(caculateList, t =>            {                Console.WriteLine("线程池计算:{0},线程ID:{1}", t, Thread.CurrentThread.ManagedThreadId);            });        }        /// <summary>        /// 直接创建Task        /// </summary>        /// <param name="caculateList"></param>        public void TaskMethod(List<int> caculateList)        {            Task[] tasks = new Task[caculateList.Count];            for (int i = 0; i < tasks.Length; i++)            {                tasks[i] = Task.Factory.StartNew(() => {                    Console.WriteLine("线程池计算:{0},线程ID:{1}", i, Thread.CurrentThread.ManagedThreadId);                });            }                      Task.WaitAll(tasks);        }    }}

ICmd

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 设计模式讲解.Command{    public abstract class ICmd    {        protected MTReceiver _MTReceiver;        public ICmd(MTReceiver _MTReceiver)        {            this._MTReceiver = _MTReceiver;        }        public abstract void Excute(List<int> caculateList);    }}

ThreadPoolCmd

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 设计模式讲解.Command{    public class ThreadPoolCmd : ICmd    {        public ThreadPoolCmd(MTReceiver _MTReceiver) : base(_MTReceiver)        {        }        public override void Excute(List<int> caculateList)        {            this._MTReceiver.ThreadPoolMethod(caculateList);        }    }}

ParallelCmd

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 设计模式讲解.Command{    public class ParallelCmd : ICmd    {        public ParallelCmd(MTReceiver _MTReceiver) : base(_MTReceiver)        {        }        public override void Excute(List<int> caculateList)        {            this._MTReceiver.ParallelMethod(caculateList);        }    }}

TaskCmd

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 设计模式讲解.Command{    public class TaskCmd : ICmd    {        public TaskCmd(MTReceiver _MTReceiver) : base(_MTReceiver)        {        }        public override void Excute(List<int> caculateList)        {            this._MTReceiver.TaskMethod(caculateList);        }    }}

Invoker

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 设计模式讲解.Command{    public class Invoker    {        private ICmd cmd;        public void SetCommand(ICmd cmd)        {            this.cmd = cmd;        }        public void ExcuteCommand(List<int> caculateList)        {            cmd.Excute(caculateList);        }    }}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表