觀察者模式之- 紅綠燈

來源:互聯網
上載者:User

namespace TrafficLights
{
    class Program
    {
        #region 定義全域變數
        static string lightcol = "";
        static TrafficLight tr = null;
        #endregion

        static void Main(string[] args)
        {
            #region 通過事件完成
            //while (true)
            //{
            //    TrafficLight tra = new TrafficLight("綠燈");

            //    Person p = new Person("Jason", tra);

            //    tra.LightBright();

            //    System.Threading.Thread.Sleep(5 * 1000);
            //    Console.WriteLine();
            //    //TrafficLight t = new TrafficLight("紅燈");
            //    //Person p1 = new Person("Mike", t);
            //    //t.LightBright();
            //    TrafficLight1 t = new TrafficLight1("紅燈");
            //    Person1 p1 = new Person1("Jason", t);
            //    t.LightBright();
            //    System.Threading.Thread.Sleep(5 * 1000);
            //    Console.WriteLine();

            //}
            #endregion

            #region 通過抽象類別完成

            //string lightcol = "";
            //while (true)
            //{
            //    System.Threading.Thread.Sleep(5 * 1000);

            //    if (lightcol == "")
            //    {
            //        lightcol = "紅";
            //    }
            //    else if (lightcol == "紅")
            //    {
            //        lightcol = "綠";
            //    }
            //    else if (lightcol == "綠")
            //    {
            //        lightcol = "紅";
            //    }

            //    TrafficLight tr = new TrafficLight();
            //    Car car = new Car(tr);
            //    Person p = new Person(tr);

            //    tr.LightBright(lightcol);
            //    Console.WriteLine();

 

            //}

          

            #endregion

            #region 通過Timer來完成

            tr = new TrafficLight();
            Car car = new Car(tr);
            Person p = new Person(tr);

            Timer time = new Timer(5 * 1000);
            time.Elapsed += new ElapsedEventHandler(time_Elapsed);
            time.Start();

            Console.Read();

 

            #endregion
        }

        #region 通過Timer 來完成
        static void time_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (lightcol == "")
            {
                lightcol = "紅";
            }
            else if (lightcol == "紅")
            {
                lightcol = "綠";
            }
            else if (lightcol == "綠")
            {
                lightcol = "紅";
            }
            tr.LightBright(lightcol);
            Console.WriteLine();
        }
        #endregion
    }

    #region 通過事件完成
    ////過馬路的人
    //public class Person
    //{
    //    private string name;

    //    public Person()
    //    { }
    //    public Person(string name, TrafficLight tra)
    //    {
    //        this.name = name;
    //        tra.lightEvent += new EventHandler<LightEventArgs>(tra_lightEvent);
           
    //    }

    //    void RedLight(object sender, LightEventArgs e)
    //    {
    //        Stop();
    //    }

    //    public void Stop()
    //    {
    //        Console.WriteLine(name + "站在原地");
    //    }

    //    void tra_lightEvent(object sender, LightEventArgs e)
    //    {
    //        Run();
    //    }

    //    private void Run()
    //    {
    //        Console.WriteLine(name + "過馬路");
    //    }
    //}

    //public class Person1
    //{
    //    private string name;

    //    public Person1()
    //    { }
    //    public Person1(string name, TrafficLight1 tra)
    //    {
    //        this.name = name;
    //        tra.lightEvent += new EventHandler<LightEventArgs1>(RedLight);

    //    }

    //    void RedLight(object sender, LightEventArgs1 e)
    //    {
    //        Stop();
    //    }

       

    //    public void Stop()
    //    {
    //        Console.WriteLine(name + "站在原地");
    //    }

    //}

    ////燈
    //public class TrafficLight
    //{
    //    //燈的名字
    //    private string lightname;

    //    //燈的事件
    //    public event EventHandler<LightEventArgs> lightEvent;

    //    //建構函式
    //    public TrafficLight()
    //    { }

    //    public TrafficLight(string name)
    //    {
    //        this.lightname = name;
    //    }

    //    //燈亮觸發的事件
    //    public void LightBright()
    //    {
    //        LightEventArgs args = new LightEventArgs(lightname);
    //        Console.WriteLine(lightname);
    //        lightEvent(this, args);
    //    }

    //}

    ////燈亮的事件
    //public class LightEventArgs : EventArgs
    //{
    //    //燈的名
    //    private string lightname;

    //    //建構函式
    //    public LightEventArgs()
    //    { }

    //    public LightEventArgs(string name)
    //    {
    //        this.lightname = name;
    //    }

    //    //輸出參數的內容
    //    public override string ToString()
    //    {
    //        return lightname + " 亮了 !";
    //    }

       
    //}

    //public class TrafficLight1
    //{
    //    //燈的名字
    //    public string lightname;

    //    //燈的事件
    //    public event EventHandler<LightEventArgs1> lightEvent;

    //    //建構函式
    //    public TrafficLight1()
    //    { }

    //    public TrafficLight1(string name)
    //    {
    //        this.lightname = name;
    //    }

    //    //燈亮觸發的事件
    //    public void LightBright()
    //    {
    //        LightEventArgs1 args = new LightEventArgs1(lightname);
    //        Console.WriteLine(lightname);
    //        lightEvent(this, args);
    //    }

    //}

    ////燈亮的事件
    //public class LightEventArgs1 : EventArgs
    //{
    //    //燈的名
    //    private string lightname;

    //    //建構函式
    //    public LightEventArgs1()
    //    { }

    //    public LightEventArgs1(string name)
    //    {
    //        this.lightname = name;
    //    }

    //    //輸出參數的內容
    //    public override string ToString()
    //    {
    //        return lightname + " 亮了 !";
    //    }

    //}
    #endregion

    #region 通過抽象類別完成

    #region 定義主動
    //定義主動
    public abstract class Observer
    {
        public abstract void Notice(Receive rece);
    }
    #endregion

    #region 定義接收
    //定義接收
    public abstract class Receive
    {
        public abstract void Show(string lightcolor);
    }
    #endregion

    #region 定義主動者
    //定義主動者
    public class TrafficLight : Observer
    {
        ArrayList al = new ArrayList();

        public override void Notice(Receive rece)
        {
            al.Add(rece);
        }
        /// <summary>
        /// 觸發的方法
        /// </summary>
        public void LightBright(string lightcolor)
        {
            Console.WriteLine(string.Format("現在是{0}燈亮了",lightcolor));
            foreach (Receive r  in al)
            {

                r.Show(lightcolor);
            }
        }
    }
    #endregion

    #region 定義車的接收者

    //定義接收者
    public class Car : Receive
    {
        public Car()
        { }

        public Car(Observer obs)
        {
            obs.Notice(this);
        }

        public override void Show(string lightcolor)
        {
            if (lightcolor == "紅")
            {
                Console.WriteLine("刹車,停止前進!");
            }
            else if (lightcolor == "綠")
            {
                Console.WriteLine("引擎發動,過通道!");
            }
        }
    }
   
    #endregion

    #region 定義人的接收者

    public class Person : Receive
    {
        public Person()
        { }

        public Person(Observer obs)
        {
            obs.Notice(this);
        }

        public override void Show(string lightcolor)
        {
            if (lightcolor == "紅")
            {
                Console.WriteLine("停止運動");  
            }
            else if (lightcolor == "綠")
            {
                Console.WriteLine("過馬路");
            }
        }
    }

    #endregion

    #endregion
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.