C# 委托事件, 发布者订阅者模式简单的demo

    public class Publisher
    {
        public void Print()
        { 
            Console.WriteLine("来活了");
        }
    }
    class Subscriber
    {
        public delegate void NumManipulationHandler();

        public event NumManipulationHandler DoJobEvent;

        public void StatusChanged()
        {
            if (DoJobEvent != null)
            {
                DoJobEvent();
                Console.WriteLine("唉,又要干活了。。。");
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("还没来活呀。。。");
                Console.ReadKey();
            }
        }
    }
    public class Program
    {
        static void Main(string[] args)
        {
            Publisher publisher = new Publisher();
            Subscriber subscriber = new Subscriber();

            subscriber.DoJobEvent += publisher.Print;

            subscriber.StatusChanged();
        }
    }

 

上一篇:【CSAPP】 lab1 datalab


下一篇:csapp 信息的表示和处理