zoukankan      html  css  js  c++  java
  • 观察者模式

    //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<string, Clock> clocks = new Dictionary<string, Clock>();
    
            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();
        }
    }
  • 相关阅读:
    关于JSON可能出现的错误,待更/todo
    mongoose的安装与使用(书签记录) 2017
    HTTP的学习记录3--HTTPS和HTTP
    HTTP的学习记录(二)头部
    HTTP(一)概述
    LeetCode 455. Assign Cookies
    LeetCode 453. Minimum Moves to Equal Array Elements
    LeetCode 448. Find All Numbers Disappeared in an Array
    LeetCode 447. Number of Boomerangs
    LeetCode 416. Partition Equal Subset Sum
  • 原文地址:https://www.cnblogs.com/skyloverdan/p/9303618.html
Copyright © 2011-2022 走看看