博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
观察者模式
阅读量:5095 次
发布时间:2019-06-13

本文共 4015 字,大约阅读时间需要 13 分钟。

//Main方法,测试方法运行正常using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ObserverClock{    class Program    {        static void Main(string[] args)        {            UtcTime utcTime = new UtcTime();            utcTime.attach("北京", new CityClock(8));            utcTime.attach("伦敦", new CityClock(0));            utcTime.attach("墨西哥", new CityClock(4));            utcTime.attach("悉尼", new CityClock(10));            utcTime.attach("纽约", new CityClock(-5));            PhoneClock phoneClock = new PhoneClock(8);            phoneClock.setUtcTime(utcTime);            phoneClock.setLocalTime(9);            utcTime.showAllClocks();            Console.ReadLine();        }    }}//UtcTime,用于设置时区时间和显示时区时间using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ObserverClock{    public class UtcTime : TimeSubject    {        private int uctZeroTime;               public int UctZeroTime        {            get { return uctZeroTime; }            set {                 uctZeroTime = value;                notifyAllClocks();            }        }                      public override void notifyAllClocks()        {            foreach (Clock clock in base.clocks.Values)            {                clock.setLocalTimeFromUtcZeroTime(this.uctZeroTime);            }        }        public void showAllClocks()        {             foreach (string CityName in base.clocks.Keys)            {                Console.WriteLine(CityName + ":" + base.clocks[CityName].localTime);            }        }    }}//PhoneClock,Clock的继承者,用于设置手机时间,从而将所有时区的时间设置using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ObserverClock{    public class PhoneClock : Clock    {        public PhoneClock(int utcOffset)            : base(utcOffset)        {        }        private UtcTime utcTime;        public override void setLocalTime(int localTime)        {            base.localTime = localTime;            utcTime.UctZeroTime = localTime - UTC_OFFSET;        }        public void setUtcTime(UtcTime _utcTime)        {            utcTime = _utcTime;        }    }}//CityClock,Clock的继承者,用于设置对应的时区时间using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ObserverClock{    public class CityClock : Clock    {        public CityClock( int utcOffset): base(utcOffset)        {                  }        public override void setLocalTime(int localTime)        {            base.localTime = localTime;        }    }}//Clock类,被观察者using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ObserverClock{    //抽象类    public abstract class Clock    {        public Clock()        {         }        public int UTC_OFFSET = 0;              public int localTime = 0;        public Clock(int utcOffset)        {            UTC_OFFSET = utcOffset;        }        public abstract void setLocalTime(int localTime);        public void setLocalTimeFromUtcZeroTime(int utcZeroTime)        {            this.localTime = Clock.MakeHourWithin0To23(utcZeroTime + this.UTC_OFFSET);        }        private static int MakeHourWithin0To23(int hour)        {            return (hour + 24) % 24;        }    }}//TimeSubject类,观察者,用于添加和移除被观察者using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ObserverClock{    public abstract class TimeSubject    {        protected Dictionary
clocks = new Dictionary
(); public void attach(string cityName, Clock clock) { clocks.Add(cityName, clock); } public void detach(string cityName, Clock clock) { clocks.Remove(cityName); } public abstract void notifyAllClocks(); }}

 

转载于:https://www.cnblogs.com/skyloverdan/p/9303618.html

你可能感兴趣的文章
阿里云服务器CentOS6.9安装Mysql
查看>>
剑指offer系列6:数值的整数次方
查看>>
js 过滤敏感词
查看>>
poj2752 Seek the Name, Seek the Fame
查看>>
软件开发和软件测试,我该如何选择?(蜗牛学院)
查看>>
基本封装方法
查看>>
生活大爆炸之何为光速
查看>>
[Typescript] Specify Exact Values with TypeScript’s Literal Types
查看>>
Illustrated C#学习笔记(一)
查看>>
理解oracle中连接和会话
查看>>
Scrapy实战篇(三)之爬取豆瓣电影短评
查看>>
HDU 5510 Bazinga KMP
查看>>
[13年迁移]Firefox下margin-top问题
查看>>
Zookeeper常用命令 (转)
查看>>
Enterprise Library - Data Access Application Block 6.0.1304
查看>>
重构代码 —— 函数即变量(Replace temp with Query)
查看>>
Bootstrap栅格学习
查看>>
程序员的数学
查看>>
聚合与组合
查看>>
洛谷 P2089 烤鸡【DFS递归/10重枚举】
查看>>