Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Blog       xmfdsh = new MyBlog("xmfdsh", "发布了一篇新博客");
            Subscriber wnm    = new Subscriber("王尼玛");
            Subscriber tmr    = new Subscriber("唐马儒");
            Subscriber wmt    = new Subscriber("王蜜桃");
            Subscriber anm    = new Subscriber("敖尼玛");

            // 添加订阅者
            xmfdsh.AddObserver(new NotifyEventHandler(wnm.Receive));
            xmfdsh.AddObserver(new NotifyEventHandler(tmr.Receive));
            xmfdsh.AddObserver(new NotifyEventHandler(wmt.Receive));
            xmfdsh.AddObserver(new NotifyEventHandler(anm.Receive));

            xmfdsh.Update();

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("移除订阅者王尼玛");
            xmfdsh.RemoveObserver(new NotifyEventHandler(wnm.Receive));
            xmfdsh.Update();

            Console.ReadLine();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            //观察者模式
            Blog       xmf = new MyBlog("苏翅", $"发表了一张照片,点击链接查看!");
            SubScriber wnn = new SubScriber("王妮娜");
            SubScriber tmr = new SubScriber("唐马儒");
            SubScriber wmt = new SubScriber("王蜜桃");
            SubScriber anm = new SubScriber("敖尼玛");

            // 添加订阅者
            xmf.AddObserver(new NotifyEventHandler(wnn.Receive));
            xmf.AddObserver(new NotifyEventHandler(tmr.Receive));
            xmf.AddObserver(new NotifyEventHandler(wmt.Receive));
            xmf.AddObserver(new NotifyEventHandler(anm.Receive));

            xmf.Update();

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();

            xmf.RemoveObserver(new NotifyEventHandler(wnn.Receive));
            Console.WriteLine($"移除订阅者{wnn.Name}");
            xmf.Update();

            Console.ReadKey();
            return;

            IOprFactory oprsub = new SubFactory();
            IOprFactory opradd = new AddFactory();
            Opreator    otsub  = oprsub.CreateOperation();

            otsub.A = 33;
            otsub.B = 22;
            Opreator otadd = opradd.CreateOperation();

            otadd.A = 8;
            otadd.B = 16;
            Trace.WriteLine($"SUB:{otsub.A}-{otsub.B}={otsub.GetResult()}");
            Trace.WriteLine($"ADD:{otadd.A}+{otadd.B}={otadd.GetResult()}");
            //A a = new A("1");
            //A aa = new A() { b = "1" };
            //Console.WriteLine(aa.b + a.b);
            //Console.ReadKey();
            //return;
            IDoctor d = new DoctorProvider();

            Trace.WriteLine($"old driver name is { d.GetDoctorName()} and {d.GetDoctorAge()} age now!!");
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            Blog xmfdsh           = new MyBlog("xmfdsh", "发布了一篇新博客");
            List <Subscriber> sub = new List <Subscriber>()
            {
                new Subscriber("王尼玛"),
                new Subscriber("唐马儒"),
                new Subscriber("王蜜桃"),
                new Subscriber("敖尼玛")
            };

            // 添加订阅者
            foreach (var item in sub)
            {
                xmfdsh.AddObserver(new NotifyEventHandler(item.Receive));
            }
            xmfdsh.Update();

            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("移除订阅者王尼玛");
            foreach (var item in sub)
            {
                if (item.Name.Equals("王尼玛"))
                {
                    xmfdsh.RemoveObserver(new NotifyEventHandler(sub[sub.IndexOf(item)].Receive));
                    break;
                }
            }
            xmfdsh.Update();

            while (true)
            {
                Console.WriteLine("请选择操作,1:添加订阅者,2:移除订阅者,N:终止操作");
                var op  = Console.ReadLine();
                var ops = new List <string>()
                {
                    "1", "2", "N"
                };
                if (!ops.Contains(op))
                {
                    Console.WriteLine("输入命令操作不合法,请重新输入!");
                }
                if (op == "1")
                {
                    Console.WriteLine("请输入您要添加的订阅者姓名");
                    var        name   = Console.ReadLine();
                    Subscriber reader = new Subscriber(name);
                    sub.Add(reader);
                    xmfdsh.AddObserver(new NotifyEventHandler(reader.Receive));
                    Console.WriteLine("{0}订阅者添加成功!", name);
                }
                if (op == "2")
                {
                    Console.WriteLine("请输入您要移除的订阅者姓名");
                    var        name   = Console.ReadLine();
                    Subscriber reader = new Subscriber(name);
                    foreach (var item in sub)
                    {
                        if (item.Name.Equals(name))
                        {
                            xmfdsh.RemoveObserver(new NotifyEventHandler(sub[sub.IndexOf(item)].Receive));
                            break;
                        }
                    }
                    Console.WriteLine("{0}订阅者移除成功!", name);
                }
                if (op == "N")
                {
                    xmfdsh.Update();
                    break;
                }
            }
        }