观察者模式之 红绿灯

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
}

原文地址:https://www.cnblogs.com/jasonjiang/p/1763453.html