观察者模式场景拓展

using System.Collections.Generic;
 
/*************************************************************************
*GUID:cab36d71-e44a-4d09-8bfd-2eef0df731ca 
*CLR:4.0.30319.17929
*Machine:  USER-KVT0SIKF25
*Author:Hollson
*Email:[email protected]
*Time:2014/3/4 12:12:52
*Code Caption:
***************************************************************************/
namespace Demo
{
    #region  气象站天气预报短信订阅服务
    interface ISmsServer
    {
        void SubScribe(ISmsClient s);
        void UnSubScribe(ISmsClient s);
        void Send(string sms);
    }
 
    public interface ISmsClient
    {
        void Receive(string sms);
    }
 
    public class SmsServer : ISmsServer
    {
        private List<ISmsClient> smss = new List<ISmsClient>();
 
        public void SubScribe(ISmsClient s)
        {
            smss.Add(s);
        }
 
        public void UnSubScribe(ISmsClient s)
        {
            if (smss.Count > 0)
            {
                smss.Remove(s);
            }
        }
 
        public void Send(string s)
        {
            foreach (var item in smss)
            {
                item.Receive(s);
            };
        }
    }
    #endregion
}

using System;
using System.Threading;
 
/*************************************************************************
*GUID:98a25f5a-15a5-4a89-ba9f-47b39be945b9 
*CLR:4.0.30319.17929
*Machine:  USER-KVT0SIKF25
*Author:Hollson
*Email:[email protected]
*Time:2014/3/4 12:13:54
*Code Caption:
***************************************************************************/
namespace Demo
{
    #region 各种对短信的操作
 
    /// 
    /// 农业电视台
    /// 
    public class AgricultureTV : ISmsClient
    {
        private string tv = string.Empty;
        public AgricultureTV(string tvName)
        {
            tv = tvName;
        }
        public void Receive(string sms)
        {
            Console.WriteLine(string.Format("天气预报:今天{0}_ _ {1}", sms, tv));
        }
    }
 
    /// 
    /// 地理信息应用订阅短信
    /// 
    public class Gis : ISmsClient
    {
        public void Receive(string sms)
        {
            Console.WriteLine("气象地图:数据已更新!");
        }
    }
 
    /// 
    /// 个人订阅短信
    /// 
    public class User : ISmsClient
    {
        private string usrName = string.Empty;
        public User(string usrName)
        {
            this.usrName = usrName;
        }
        public void Receive(string sms)
        {
            if (sms == "晴")
            {
                Console.WriteLine(usrName + " 说:“呵呵,今天的天气不错,出去泡妞了。。。”");
            }
        }
    }
 
    #endregion
 
    /// 
    /// 演示
    /// 
    public class Pro
    {
        static void Main(string[] args)
        {
            SmsServer ssr = new SmsServer();
            ssr.SubScribe(new AgricultureTV("CCTV5"));
            ssr.SubScribe(new User("张 三"));
            //ssr.SubScribe(new User("李 四"));  
            ssr.SubScribe(new Gis());
 
 
            while (true)
            {
                int r = new Random().Next(0, 3);
                if (r == 0)
                    ssr.Send("晴");
                if (r == 1)
                {
                    ssr.Send("雨");
                }
                if (r == 2)
                {
                    ssr.Send("霾");
                }
                Console.WriteLine();
                Thread.Sleep(3000);
            }
        }
    }
}

你可能感兴趣的:(观察者模式场景拓展)