• 观察者模式场景拓展


    using System.Collections.Generic;
     
    /*************************************************************************
    *GUID:cab36d71-e44a-4d09-8bfd-2eef0df731ca 
    *CLR:4.0.30319.17929
    *Machine:  USER-KVT0SIKF25
    *Author:Hollson
    *Email:Hollson@QQ.com
    *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:Hollson@QQ.com
    *Time:2014/3/4 12:13:54
    *Code Caption:
    ***************************************************************************/
    namespace Demo
    {
        #region 各种对短信的操作
     
        /// <summary>
        /// 农业电视台
        /// </summary>
        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));
            }
        }
     
        /// <summary>
        /// 地理信息应用订阅短信
        /// </summary>
        public class Gis : ISmsClient
        {
            public void Receive(string sms)
            {
                Console.WriteLine("气象地图:数据已更新!");
            }
        }
     
        /// <summary>
        /// 个人订阅短信
        /// </summary>
        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
     
        /// <summary>
        /// 演示
        /// </summary>
        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);
                }
            }
        }
    }
    
  • 相关阅读:
    double相加(減)结果会有些误差
    创建表,操作表
    删除数据库
    DDL语句
    HCDA-12-配置基本静态路由
    HCDA-11-配置直连路由
    1-5配置STelnet登录系统
    HCDA-9-管理设备文件系统
    HCDA-8-了解设备系统文件
    Java生成随机汉字
  • 原文地址:https://www.cnblogs.com/Hollson/p/3580122.html
Copyright © 2020-2023  润新知