Example #1
0
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("欢迎来到.net高级班VIP课程,今天是Eleven老师为大家带来的委托事件的学习");
                //MyDelegate myDelegate = new MyDelegate();
                //myDelegate.Show();

                //CarFactory carFactory = new CarFactory();
                ////carFactory.BuildCar(EngineType.NaturalInspiration);
                ////carFactory.BuildCar(EngineType.Turbo);
                ////carFactory.BuildCar(EngineType.Electric);

                //CarFactory.BuildEngineDelegate method = new CarFactory.BuildEngineDelegate
                //(carFactory.BuildEngineNaturalInspiration);

                //carFactory.BuildCar(method);

                //CarFactory.SafeInvoke(() =>
                //    {
                //        carFactory.BuildCar(method);
                //    });

                {
                    Console.WriteLine("************cat.Miao();***********");
                    Cat cat = new Cat();
                    cat.Miao();


                    Console.WriteLine("************cat.MiaoPattern();***********");
                    cat.AddObject(new Dog());
                    cat.AddObject(new Mouse());
                    cat.AddObject(new Baby());
                    cat.AddObject(new Mother());
                    cat.AddObject(new Brother());
                    cat.AddObject(new Father());
                    cat.AddObject(new Neighbor());

                    cat.MiaoPattern();



                    Console.WriteLine("************cat.MiaoDelegate();***********");
                    cat.MiaoAction += new Dog().Wang;
                    cat.MiaoAction += new Mouse().Run;
                    cat.MiaoAction += new Baby().Cry;
                    cat.MiaoAction += new Mother().Wispher;
                    cat.MiaoAction += new Brother().Turn;
                    cat.MiaoAction += new Father().Roar;
                    cat.MiaoAction += new Neighbor().Awake;
                    cat.MiaoAction += new Stealer().Hide;

                    cat.MiaoAction.Invoke();
                    cat.MiaoAction = null;


                    cat.MiaoAction -= new Stealer().Hide;
                    cat.MiaoDelegate();


                    Console.WriteLine("************cat.MiaoEvent();***********");
                    cat.MiaoActionEvent += new Dog().Wang;//订阅
                    cat.MiaoActionEvent += new Mouse().Run;
                    cat.MiaoActionEvent += new Baby().Cry;
                    cat.MiaoActionEvent += new Mother().Wispher;
                    cat.MiaoActionEvent += new Brother().Turn;
                    cat.MiaoActionEvent += new Father().Roar;
                    cat.MiaoActionEvent += new Neighbor().Awake;

                    //cat.MiaoActionEvent.Invoke();
                    //cat.MiaoActionEvent = null;

                    cat.MiaoAction      -= new Stealer().Hide;//取消订阅
                    cat.MiaoActionEvent += new Stealer().Hide;

                    cat.MiaoEvent();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }