Esempio n. 1
0
        static void Main(string[] args)
        {
            Publisher   pub  = new Publisher();
            Subscriber1 sub1 = new Subscriber1();
            Subscriber2 sub2 = new Subscriber2();
            Subscriber3 sub3 = new Subscriber3();

            pub.MyEvent += sub1.OnNumberChanged;
            pub.MyEvent += sub2.OnNumberChanged;
            pub.MyEvent += sub3.OnNumberChanged;

            pub.DoSomething();

            Console.WriteLine("Control back to client");


            Console.Read();
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            //Car c1 = new Car("SlugBug", 100, 10);
            ////c1.AboutToBlow += new Car.CarEngineHandler(CarIsAlmostDoomed);
            //// 当没有参数传入时可以省略掉delegate之后的()部分
            //c1.AboutToBlow += delegate(object sender, CarEventArgs e)
            //{
            //    Console.WriteLine("{0} says: {1}", sender, e.msg);
            //};
            //// 使用方法组转换,+=直接结合方法名进行操作
            //c1.AboutToBlow += CarAboutToBlow;
            //EventHandler<CarEventArgs> d = new EventHandler<CarEventArgs>(CarExploaded);
            //c1.Exploded += d;

            //for (int i = 0; i < 6; i++)
            //{
            //    c1.Accelerate(20);
            //}

            //c1.Exploded -= CarExploaded;
            //c1.AboutToBlow -= CarAboutToBlow;
            //for (int i = 0; i < 6; i++)
            //{
            //    c1.Accelerate(20);
            //}

            Shape shape = new Shape();
            Subscriber1 sub = new Subscriber1(shape);
            Subscribe2 sub2 = new Subscribe2(shape);
            shape.Draw();
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }