コード例 #1
0
        public static void Show()
        {
            {
                Cat cat = new Cat();
                cat.Miao();
            }
            {
                Cat cat = new Cat();
                //
                cat.AddObserver(new Chicken());
                cat.AddObserver(new Baby());
                cat.AddObserver(new Brother());
                cat.AddObserver(new Dog());
                cat.AddObserver(new Father());
                cat.AddObserver(new Mother());
                cat.AddObserver(new Neighbor());
                cat.AddObserver(new Stealer());
                cat.AddObserver(new Mouse());

                cat.MiaoObserver();
            }
            //甩锅了,那锅还是存在的---这个锅,行为型设计模式,不背!--创建型设计模式
            //观察者之后,观察者数量可以增减 而且可以调整顺序
            //
            {
                Cat cat = new Cat();
                cat.MiaoHandler += (new Chicken().Woo);
                cat.MiaoHandler += (new Baby().Cry);
                cat.MiaoHandler += (new Brother().Turn);
                cat.MiaoHandler += (new Dog().Wang);
                cat.MiaoHandler += (new Father().Roar);
                cat.MiaoHandler += (new Mother().Whisper);
                cat.MiaoHandler += (new Neighbor().Awake);
                cat.MiaoHandler += (new Stealer().Hide);
                cat.MiaoHandler += (new Mouse().Run);
                //为什么有这么好的事儿? 这世界上其实没有这么好的事儿,
                //哪里有什么岁月静好,是因为有人替你负重前行
                cat.MiaoHandler += new Action(new Mouse().Run);
                //Action是委托,,委托实际上就是类
                cat.MiaoObserver();
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: EasyCase/MyDelegateEvent
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("欢迎来到.net高级班VIP课程,今天是Eleven老师为大家带来的委托事件的学习");
                #region 委托
                {
                    //Console.WriteLine("****************************MyDelegate*************************");
                    MyDelegate myDelegate = new MyDelegate();
                    myDelegate.Show();
                }

                {
                    //Console.WriteLine("****************************委托解耦,代码复用*************************");
                    //Student student = new Student()
                    //{
                    //    Id = 123,
                    //    Name = "Rabbit",
                    //    Age = 23,
                    //    ClassId = 1
                    //};
                    //Student student1;
                    ////上端还不是得知道是哪个国家的人?
                    //student.Study();
                    //student.SayHi("大脸猫", PeopleType.Chinese);

                    //student.SayHi("icefoxz", PeopleType.Malaysia);

                    //student.SayHiChinese("大脸猫");

                    //{
                    //    SayHiDelegate method = new SayHiDelegate(student.SayHiChinese);
                    //    student.SayHiPerfact("大脸猫", method);
                    //}
                    //{
                    //    SayHiDelegate method = new SayHiDelegate(student.SayHiAmerican);
                    //    student.SayHiPerfact("PHS", method);
                    //}
                    //{
                    //    SayHiDelegate method = new SayHiDelegate(student.SayHiMalaysia);
                    //    student.SayHiPerfact("icefoxz", method);
                    //}
                }
                #endregion

                #region Action Func
                {
                    Console.WriteLine("****************************ActionFunc*************************");
                    MyDelegate myDelegate = new MyDelegate();
                    myDelegate.Show();
                }
                {
                    Console.WriteLine("****************************Event*************************");
                    Cat cat = new Cat();
                    cat.Miao();

                    Console.WriteLine("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
                    cat.CatMiaoAction += new Dog().Wang;
                    cat.CatMiaoAction += new Mouse().Run;
                    cat.CatMiaoAction += new Baby().Cry;
                    cat.CatMiaoAction += new Mother().Wispher;

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

                    cat.CatMiaoAction += new Brother().Turn;
                    cat.CatMiaoAction += new Father().Roar;
                    cat.CatMiaoAction += new Neighbor().Awake;
                    cat.CatMiaoAction += new Stealer().Hide;
                    cat.MiaoDelegate();
                    //去除依赖,Cat稳定了;还可以有多个Cat实例


                    Console.WriteLine("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
                    cat.CatMiaoActionHandler += new Dog().Wang;
                    cat.CatMiaoActionHandler += new Mouse().Run;
                    cat.CatMiaoActionHandler += new Baby().Cry;

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

                    cat.CatMiaoActionHandler += new Mother().Wispher;
                    cat.CatMiaoActionHandler += new Brother().Turn;
                    cat.CatMiaoActionHandler += new Father().Roar;
                    cat.CatMiaoActionHandler += new Neighbor().Awake;
                    cat.CatMiaoActionHandler += new Stealer().Hide;
                    cat.MiaoEvent();


                    Console.WriteLine("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
                    cat.AddObserver(new Dog());
                    cat.AddObserver(new Mouse());
                    cat.AddObserver(new Baby());
                    cat.AddObserver(new Mother());
                    cat.AddObserver(new Brother());
                    cat.AddObserver(new Father());
                    cat.AddObserver(new Neighbor());
                    cat.AddObserver(new Stealer());
                    cat.MiaoObserver();
                }
                #endregion
                {
                    EventStandard.Show();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: huruiyi/DotNet-Draft
    private static void Main(string[] args)
    {
        try
        {
            Console.WriteLine("欢迎来到.net高级班公开课之设计模式特训,今天是Eleven老师为大家带来的观察者模式");

            {
                Cat cat = new Cat();
                cat.Miao();

                Console.WriteLine("*************Observer***************");
                {
                    Brother brother = new Brother();

                    cat.AddObserver(new Mouse());
                    cat.AddObserver(new Dog());
                    cat.AddObserver(new Cricket());
                    cat.AddObserver(new Baby());
                    cat.AddObserver(new Father());
                    cat.AddObserver(new Mother());
                    cat.AddObserver(brother);
                    cat.AddObserver(new Neighbor());
                    cat.AddObserver(new Stealer());

                    cat.MiaoObserver();

                    cat.RemoveObserver(brother);
                    cat.MiaoObserver();
                }
                {
                    Console.WriteLine("*************Event***************");

                    Brother brother = new Brother();

                    cat.CatMiaoEvent += new Mouse().Run;
                    cat.CatMiaoEvent += () => new Dog().Wang("3");
                    cat.CatMiaoEvent += new Cricket().Sing;
                    cat.CatMiaoEvent += new Baby().Cry;
                    cat.CatMiaoEvent += new Father().Roar;
                    cat.CatMiaoEvent += new Mother().Whisper;
                    cat.CatMiaoEvent += brother.Turn;
                    cat.CatMiaoEvent += new Neighbor().Awake;
                    cat.CatMiaoEvent += new Stealer().Hide;
                    if (true)
                    {
                        AddDog(cat);
                    }

                    cat.MiaoEvent();

                    cat.CatMiaoEvent -= brother.Turn;

                    cat.MiaoEvent();
                }
            }
            {
                Console.WriteLine("*************Another Cat***************");
                Cat cat = new Cat();

                Brother brother = new Brother();

                cat.AddObserver(new Baby());
                cat.AddObserver(new Father());
                cat.AddObserver(new Mother());
                cat.AddObserver(brother);
                cat.AddObserver(new Neighbor());

                cat.MiaoObserver();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        Console.Read();
    }
コード例 #4
0
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("================委托========================");

                #region 委托
                {
                    MyDelegate myDelegate = new MyDelegate();
                    //myDelegate.Show();
                }
                #endregion

                #region 委托解耦,代码复用
                {
                    Console.WriteLine("==========委托解耦,代码复用==========");
                    Student student = new Student()
                    {
                        Id      = 123,
                        Name    = "Rabbit",
                        Age     = 23,
                        ClassId = 1
                    };
                    //上端还不是得知道是哪个国家的人?
                    student.Study();
                    student.SayHi("大脸猫", PeopleType.Chinese);
                    student.SayHi("icefoxz", PeopleType.Malaysia);
                    student.SayHiChinese("大脸猫");
                    {
                        Action <string> method = student.SayHiChinese;
                        student.SayHiPerfact("大脸猫", method);
                    }
                    {
                        Action <string> method = student.SayHiAmerican;
                        student.SayHiPerfact("PHS", method);
                    }
                    {
                        Action <string> method = student.SayHiMalaysia;
                        student.SayHiPerfact("icefoxz", method);
                    }
                }
                #endregion


                #region Action Func
                {
                    Console.WriteLine("****************************Event*************************");
                    Cat cat = new Cat();
                    cat.Miao();

                    Console.WriteLine("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
                    cat.CatMiaoAction += new Dog().Wang;
                    cat.CatMiaoAction += new Mouse().Run;
                    cat.CatMiaoAction += new Baby().Cry;
                    cat.CatMiaoAction += new Mother().Wispher;

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

                    cat.CatMiaoAction += new Brother().Turn;
                    cat.CatMiaoAction += new Father().Roar;
                    cat.CatMiaoAction += new Neighbor().Awake;
                    cat.CatMiaoAction += new Stealer().Hide;
                    cat.MiaoDelegate();
                    //去除依赖,Cat稳定了;还可以有多个Cat实例


                    Console.WriteLine("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
                    cat.CatMiaoActionHandler += new Dog().Wang;
                    cat.CatMiaoActionHandler += new Mouse().Run;
                    cat.CatMiaoActionHandler += new Baby().Cry;

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

                    cat.CatMiaoActionHandler += new Mother().Wispher;
                    cat.CatMiaoActionHandler += new Brother().Turn;
                    cat.CatMiaoActionHandler += new Father().Roar;
                    cat.CatMiaoActionHandler += new Neighbor().Awake;
                    cat.CatMiaoActionHandler += new Stealer().Hide;
                    cat.MiaoEvent();


                    Console.WriteLine("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
                    cat.AddObserver(new Dog());
                    cat.AddObserver(new Mouse());
                    cat.AddObserver(new Baby());
                    cat.AddObserver(new Mother());
                    cat.AddObserver(new Brother());
                    cat.AddObserver(new Father());
                    cat.AddObserver(new Neighbor());
                    cat.AddObserver(new Stealer());
                    cat.MiaoObserver();
                }
                #endregion
                {
                    //EventStandard.Show();
                }
            }
            catch (Exception Ex)
            {
                Console.WriteLine("错误信息:" + Ex.Message);
            }
            Console.WriteLine("结束");
            Console.ReadLine();
        }