文章出處

觀察模式:定義對象間的一種一對多的依賴關系,當一個對象的狀態發生改變時, 所有依賴于它的對象都得到通知并被自動更新;

以下的列子含義:當車站要求指定的列車發車,所有在站內的列表均得到發車通知!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PatternProject
{
    public interface ITrain
    {
        string From
        { get; set; }

        string To
        { get; set; }

        DateTime StartingDateTime
        { get; set; }

        void Start(ITrain train);
    }

    public interface IStation
    {
        void AddTrain(ITrain train);
        void CancelTrain(ITrain train);
        void StartNotify(ITrain train);
    }

    public class Station : IStation
    {
        List<ITrain> _trainList = new List<ITrain>();

        public void AddTrain(ITrain train)
        {
            _trainList.Add(train);
        }

        public void CancelTrain(ITrain train)
        {
            _trainList.Remove(train);
        }

        public void StartNotify(ITrain train)
        {
            foreach (ITrain t in _trainList)
            {
                t.Start(train);
            }
        }
    }

    public class Train : ITrain
    {
       public string From
        { get; set; }

       public string To
        { get; set; }

       public DateTime StartingDateTime
        { get; set; }

       public Train(string from, string to, DateTime startingDateTime)
       {
           this.From = from;
           this.To = to;
           this.StartingDateTime = startingDateTime;
       }

       public bool IsCurrent(ITrain train)
       {
           if (train.From == this.From && train.To == this.To && train.StartingDateTime == this.StartingDateTime)
           {
               return true;
           }
           else
           {
               return false;
           }
       }

       public void Start(ITrain train)
       {
           Console.WriteLine("{0}開往{1}列車收到通知:",this.From,this.To);
           if (IsCurrent(train))
           {
               Console.WriteLine("從{0}開往{1}的列車,于{2}開車,敬請注意!", this.From, this.To, this.StartingDateTime);
           }
           else
           {
               Console.WriteLine("還未到開車時間,敬請稍候!");
           }
       }
    }


}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PatternProject
{
    class Program
    {
        static void Main(string[] args)
        {

            Station station = new Station();
            
            station.AddTrain(new Train("北京", "上海", new DateTime(2014, 5, 1)));
            station.AddTrain(new Train("北京", "武漢", new DateTime(2014, 3, 1)));
            station.AddTrain(new Train("武漢", "上海", new DateTime(2014, 2, 1)));
            station.AddTrain(new Train("上海", "南京", new DateTime(2014, 8, 1)));

            Train t = new Train("北京", "武漢", new DateTime(2014, 3, 1));

            station.StartNotify(t);

            Console.ReadKey();
        }
    }
}

  


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()