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); } } } }
The Times 03/Jan/2009 Chancellor on brink of second bailout for banks
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性