コード例 #1
0
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("欢迎来到.net高级班公开课之设计模式特训,今天是Eleven老师为大家带来的的状态模式");
                {
                    TrafficLight light = new TrafficLight()
                    {
                        Color = LightColor.Green
                    };
                    light.Show();
                    light.Turn();
                    light.Show();
                    light.Turn();
                    light.Show();
                    light.Turn();
                }
                Console.WriteLine("******************************");
                {
                    LightBase light = new LightGreen();
                    light.Show();
                    light.Turn();
                    light.Show();
                    light.Turn();
                }
                Console.WriteLine("******************************");
                {
                    LightBase light   = new LightGreen();
                    Context   context = new Context()
                    {
                        CurrentLight = light
                    };

                    context.Show();
                    context.Turn();
                    context.Show();
                    context.Turn();
                    context.Show();
                    context.Turn();
                    context.Show();
                    context.Turn();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }