码迷,mamicode.com
首页 > 其他好文 > 详细

Observer Pattern

时间:2016-06-18 23:44:10      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

                                                                    Observer Pattern

通知者接口:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace Ando.Observer
 7 {
 8     public interface ISender
 9     {
10          void Add( Observer observer);
11 
12         void Remove(Observer observer);
13 
14         void Notify();
15 
16         string  SenderInfo { get; set; }
17     }
18 }

 

 

具体通知者:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ando.Observer
{
   public class ConcreteSender:ISender
    {
       private  IList<Observer>  observerList=new List<Observer>();

       public void Add(Observer observer)
       {
           observerList.Add(observer);
       }

       public void Remove(Observer observer)
       {
           observerList.Remove(observer);
       }

       public void Notify()
       {
           foreach (Observer observer in observerList)
           {
               observer.Update();
           }
       }

       public string SenderInfo { get; set; }
    }
}

 

 

抽象接收者:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.AccessControl;
using System.Text;

namespace Ando.Observer
{
    public abstract class Observer
    {
        protected string Name;
        protected ISender Sender;

        protected Observer(string name, ISender sender)
        {
            this.Name = name;
            this.Sender = sender;
        }

        public abstract void Update();

    }
}

 

 

具体接收者:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Globalization;
 4 using System.Linq;
 5 using System.Text;
 6 
 7 namespace Ando.Observer
 8 {
 9     public class ConcreteObserver : Observer
10     {
11         public ConcreteObserver(string name, ISender sender)
12             : base(name, sender)
13         {
14         }
15 
16         public override void Update()
17         {
18             Console.WriteLine("{0} {1},", Sender.SenderInfo, Name);
19             //Console.ReadKey();
20         }
21     }
22 }

 

 

主函数代码:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Ando.Observer;

namespace MainObserver
{
    class Program
    {
        static void Main(string[] args)
        {
            ConcreteSender security = new ConcreteSender();
            ConcreteObserver workMate=new ConcreteObserver("Ando",security);
            ConcreteObserver workMate1=new ConcreteObserver("Sweet",security);
            security.Add(workMate);
            security.Add(workMate1);
            security.SenderInfo = "写代码的速度给我加快点!";
            security.Notify();
           Console.WriteLine("~~~啦啦啦,Ando 成功写出了一个观察者模式~~~");
            Console.ReadKey();
        }
    }
}

 

 

效果:

技术分享

 

Observer Pattern

标签:

原文地址:http://www.cnblogs.com/anmutu/p/5597045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!