文章

C#委托的高级使用--多播委托(Multcast)/多线程异步/Interface取代委托

来,继续拿你们剩下的700分

单播委托

代码由上至下执行,
实例:

using System;
using System.Threading;//注意这里加入了多线程 但本案例未使用
namespace Multicast
{
    class Program
    {
        static void Main(string[] args)
        {
            Students stu1 = new Students() { ID = 1, PenColor = ConsoleColor.Yellow };//创建实例,参数给出控制台颜色
            Students stu2 = new Students() { ID = 2, PenColor = ConsoleColor.Green };
            Students stu3 = new Students() { ID = 3, PenColor = ConsoleColor.Red };
            Students stu4 = new Students() { ID = 4, PenColor = ConsoleColor.Blue };
            Action act1 = new Action(stu1.DoHomeWork);
            Action act2 = new Action(stu2.DoHomeWork);
            Action act3 = new Action(stu3.DoHomeWork);
            Action act4 = new Action(stu4.DoHomeWork);
            act1();
            act2();
            act3();
            act4();

        }
    }
    class Students
    {
        public int ID { get; set; }
        public ConsoleColor PenColor { get; set; } //ConsoleColor类型在控制台输出的颜色是会不一样的
        public void DoHomeWork()
        {
            for (int i = 0; i < 5; i++)
            {
                Console.ForegroundColor = PenColor;
                Console.WriteLine("学生{0}做作业{1}小时了", ID, i);
                Thread.Sleep(500);
            }
        }
    }
}

多播委托

看,这么简单,你又拿到100分
多播委托的执行顺序,是按照你封装时候的先后顺序执行的

            act1 += act2;
            act1 += act3;
            act1 += act4;
            act1();//一个委托封装多个委托,这个就叫多播委托

隐式异步

            act1.BeginInvoke(null,null);//隐式异步调用(隐式多线程异步)第一个参数为回调函数
            act2.BeginInvoke(null, null);
            act3.BeginInvoke(null, null);
            act4.BeginInvoke(null, null);

别玩了哈 .NET 4.5开始已经不支持该方式使用多线程了.

显示异步 Old方法

            Thread TH1 = new Thread(new ThreadStart(stu1.DoHomeWork));
            Thread TH2 = new Thread(new ThreadStart(stu2.DoHomeWork));
            Thread TH3 = new Thread(new ThreadStart(stu3.DoHomeWork));
            Thread TH4 = new Thread(new ThreadStart(stu4.DoHomeWork));
            TH1.Start();
            TH2.Start();
            TH3.Start();
            TH4.Start();

显示异步 New方法

using System;
using System.Threading;
using System.Collections.Generic;
using System.Threading.Tasks;//新异步方法所需

namespace Multicast
{
    class Program
    {
        static void Main(string[] args)
        {
            Students stu1 = new Students() { ID = 1, PenColor = ConsoleColor.Yellow };//创建实例,参数给出控制台颜色
            Students stu2 = new Students() { ID = 2, PenColor = ConsoleColor.Green };
            Students stu3 = new Students() { ID = 3, PenColor = ConsoleColor.Red };
            Students stu4 = new Students() { ID = 4, PenColor = ConsoleColor.Blue };

            Task task1 = new Task(new Action(stu1.DoHomeWork));
            Task task2 = new Task(new Action(stu2.DoHomeWork));
            Task task3 = new Task(new Action(stu3.DoHomeWork));
            Task task4 = new Task(new Action(stu4.DoHomeWork));
            task1.Start();
            task2.Start();
            task3.Start();
            task4.Start();
            task1.Wait();//都不等待的话,那就看不到输出了

interface

using System;

namespace DeleGate_CallBack
{
    class Program
    {
        static void Main(string[] args)
        {
            IproductFactory pizzaFactory = new PizzaFactory();
            IproductFactory toycarFactory = new ToyCarFactory();
            WrapFactory wrapFactory = new WrapFactory();
            Box box1 = wrapFactory.WrapProduct(pizzaFactory);
            Box box2 = wrapFactory.WrapProduct(toycarFactory);
            Console.WriteLine(box1.product.Name);
            Console.WriteLine(box2.product.Name);
        }
    }
    interface IproductFactory
    {
        Product Make();

    }

    class PizzaFactory : IproductFactory  //Ctrl+. 会直接创建一个实现方法
    {
        public Product Make()
        {
            Product product = new Product();
            product.Name = "Pizza";
            product.Price = 33;
            return product;
        }
    }
    class ToyCarFactory : IproductFactory
    {
        public Product Make()
        {
            Product product = new Product();
            product.Name = "ToyCar";
            product.Price = 100;
            return product;
        }
    }

    class Logger
    {
        public void log(Product product)
        {
            Console.WriteLine("产品{0}被创建 创建时间{1},产品定价{2}", product.Name, DateTime.UtcNow, product.Price);//utcNow不带时区.且方法被调用立即打印输出
        }
    }
    class Product
    {
        public string Name { get; set; }
        public double Price { get; set; }
    }
    class Box
    {
        public Product product { get; set; }
    }

    class WrapFactory  //包装工厂
    {
        public Box WrapProduct(IproductFactory productFactory)//interface定义的类型的参数
        {
            Box box = new Box();
            Product product =productFactory.Make();
            box.product = product;
            return box;
        }

    }

}

原文来自:C#委托的高级使用--多播委托(Multcast)/多线程异步/Interface取代委托,尊重自己,尊重每一个人;转发请注明来源!
0 0

发表评论