//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 Dictionaryclocks = 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(); }}