台灯引发的乱想

 

using System;

using System.Collections.Generic;

using System.Text;

using System.Reflection;

#region

//假设台灯是一个系统,请你设计这样一个系统。只需给出设计方案和主要的类声明。

#endregion

namespace Light_Design

{

    /// <summary>

    /// 动作类型

    /// </summary>

    public enum ActionType

    {

        Hand,Remote

    }

    /// <summary>

    /// 台灯状态

    /// </summary>

     public enum Statu

     {

         Off,On,Up,Down

     };

     /// <summary>

     /// 动作属性

     /// </summary>

     [AttributeUsage(AttributeTargets.Class)]

     public class ActionAttribute : Attribute

     {

         private ActionType actionType;

 

         public ActionAttribute(ActionType type)

         {

             this.actionType = type;

         }

         public ActionType ActionOfType

         {

             get

             {

                 return actionType;

             }

             set

             {

                 actionType = value;

             }

         }

     }

    /// <summary>

    /// 状态属性

    /// </summary>

    [AttributeUsage(AttributeTargets.Class)]

    public class StatuAttribute:Attribute

    {

        private Statu statu;

 

        public StatuAttribute(Statu statu)

        {

            this.statu = statu;

        }

        public Statu ActionStatu

        {

            get

            {

                return statu;

            }

            set

            {

                statu = value;

            }

        }

    }

    /// <summary>

    /// 动作列表属性

    /// </summary>

    [AttributeUsage(AttributeTargets.Interface)]

    public class ActionListAttribute:Attribute

    {

        private Type[] actionList ;

 

        public ActionListAttribute(Type[] type)

        {

            actionList = type;

        }

        public Type[] ActionList

        {

            get

            {

                return actionList;

            }

            set

            {

                actionList = value;

            }

        }

  }

/// <summary>

    /// 动作管理列表属性

    /// </summary>

    [AttributeUsage(AttributeTargets.Interface)]

    public class ActionManagerListAttribute:Attribute

    {

        private Type[] actionList ;

 

        public ActionListAttribute(Type[] type)

        {

            actionList = type;

        }

        public Type[] ActionList

        {

            get

            {

                return actionList;

            }

            set

            {

                actionList = value;

            }

        }

    }

  

    /// <summary>

    /// 控制台灯动作

    /// </summary>

   

    public interface IAction

    {

        void SetStatu(ref Statu statu);

    }

    /// <summary>

    /// 手动控制台灯动作

    /// </summary>

    [ActionListAttribute(new Type[] { typeof(TurnOffByHand), typeof(TurnUpByHand), typeof(TurnDownByHand), typeof(TurnOnByHand) })]

    public interface IActionByHand:IAction

    {

        new void SetStatu(ref Statu statu);

    }

    /// <summary>

    /// 遥控控制台灯动作

    /// </summary>

    [ActionListAttribute(new Type[] { typeof(TurnOffByRemote), typeof(TurnUpByRemote), typeof(TurnDownByRemote), typeof(TurnOnByRemote) })]

  

    public interface IActionByRemote:IAction

    {

        new void SetStatu(ref Statu statu);

    }

    #region 手动操作台灯

 

    /// <summary>

    /// 手动开灯

    /// </summary>

    [StatuAttribute(Statu.On)]

    public class TurnOnByHand : IActionByHand

    {

        public void SetStatu(ref Statu statu)

        {

            statu = Statu.On;

        }

    }

    /// <summary>

    /// 手动关灯

    /// </summary>

    [StatuAttribute(Statu.Off)]

    public class TurnOffByHand : IActionByHand

    {

        public void SetStatu(ref Statu statu)

        {

            statu = Statu.Off;

        }

    }

    /// <summary>

    /// 手动调大亮度

    /// </summary>

    [StatuAttribute(Statu.Up)]

    public class TurnUpByHand : IActionByHand

    {

        public void SetStatu(ref Statu statu)

        {

            statu = Statu.Up;

        }

    }

    /// <summary>

    /// 手动调小亮度

    /// </summary>

    [StatuAttribute(Statu.Down)]

    public class TurnDownByHand : IActionByHand

    {

        public void SetStatu(ref Statu statu)

        {

            statu = Statu.Down;

        }

    }

    #endregion

 

    #region 遥控操作台灯

    /// <summary>

    /// 遥控关灯

    /// </summary>

    [StatuAttribute(Statu.Off)]

    public class TurnOffByRemote : IActionByRemote

    {

        public void SetStatu(ref Statu statu)

        {

            statu = Statu.Off;

        }

    }

    /// <summary>

    /// 遥控开灯

    /// </summary>

    [StatuAttribute(Statu.On)]

    public class TurnOnByRemote : IActionByRemote

    {

        public void SetStatu(ref Statu statu)

        {

            statu = Statu.On;

        }

    }

    /// <summary>

    /// 遥控调大亮度

    /// </summary>

    [StatuAttribute(Statu.Up)]

    public class TurnUpByRemote : IActionByRemote

    {

        public void SetStatu(ref Statu statu)

        {

            statu = Statu.Up;

        }

    }

    /// <summary>

    /// 遥控调小亮度

    /// </summary>

    [StatuAttribute(Statu.Down)]

    public class TurnDownByRemote : IActionByRemote

    {

        public void SetStatu(ref Statu statu)

        {

            statu = Statu.Down;

        }

    }

    #endregion

 

    /// <summary>

    /// 台灯

    /// </summary>

    public class Light

    {

        private Statu statuOfLight;

 

        public Statu StatuOfLight

        {

            get

            {

                return statuOfLight;

            }

            set

            {

                this.statuOfLight = value;

            }

        }

         /// <summary>

         /// 手动操作

         /// </summary>

         /// <param name="type">操作类型</param>

        public void HandDown(Statu type)

        {

            ActionFactory.CreateActionManager(ActionType.Hand).CreateAction(type).SetStatu(ref this.statuOfLight);

 

            OutputStatu(ActionType.Hand.ToString());

        }

       /// <summary>

       /// 遥控操作

       /// </summary>

        /// <param name="type">操作类型</param>

        public void RemoteDown(Statu type)

        {

 

            ActionFactory.CreateActionManager(ActionType.Remote).CreateAction(type).SetStatu(ref this.statuOfLight);

 

            OutputStatu(ActionType.Remote.ToString());

          

        }

 

        public void OutputStatu(string action)

        {

            Console.WriteLine("The Light is {0} By {1}", statuOfLight.ToString(),action);

        }

    }

    /// <summary>

    /// 动作管理类

  /// </summary>

  [ActionManagerListAttribute(new Type[]{typeof(HandActionManager),typeof(RemoteActionManager)}]

    public interface IActionManager

    {

 

         IAction CreateAction(Statu typeStatu);

      

    }

    /// <summary>

    /// 手动动作管理类

    /// </summary>

    [ActionAttribute(ActionType.Hand)]

    public class HandActionManger: IActionManager

    {

 

        public IAction CreateAction(Statu typeStatu)

        {

            ActionListAttribute ala = (ActionListAttribute)Attribute.GetCustomAttribute(typeof(IActionByHand), typeof(ActionListAttribute));

 

            foreach (Type type in ala.ActionList)

            {

                StatuAttribute at = (StatuAttribute)StatuAttribute.GetCustomAttribute(type, typeof(StatuAttribute));

 

                if (at.ActionStatu == typeStatu)

                {

                    Object ob = Assembly.GetExecutingAssembly().CreateInstance(type.FullName);

 

                    return ob as IAction;

                }

            }

            return null;

 

        }

    }

    /// <summary>

    /// 遥控动作管理类

    /// </summary>

    [ActionAttribute(ActionType.Remote)]

    public class RemoteActionManager: IActionManager

    {

        public IAction CreateAction(Statu typeStatu)

        {

            ActionListAttribute ala = (ActionListAttribute)Attribute.GetCustomAttribute(typeof(IActionByRemote), typeof(ActionListAttribute));

 

            foreach (Type type in ala.ActionList)

            {

                StatuAttribute at = (StatuAttribute)StatuAttribute.GetCustomAttribute(type, typeof(StatuAttribute));

 

                if (at.ActionStatu == typeStatu)

                {

                    Object ob = Assembly.GetExecutingAssembly().CreateInstance(type.FullName);

 

                    return ob as IAction;

                }

            }

            return null;

 

        }

    }

    /// <summary>

    /// 动作管理工厂

    /// </summary>

    public class ActionFactory

    {

        public static IActionManager am;

 

        public static IActionManager CreateActionManager(ActionType type)

        {

           ActionManagerListAttribute ala = (ActionListAttribute)Attribute.GetCustomAttribute(typeof(IActionManager), typeof(ActionManagerListAttribute));

 

            foreach (Type type in ala.ActionList)

            {

                ActionAttribute at = (ActionAttribute)StatuAttribute.GetCustomAttribute(type, typeof(ActionAttribute));

 

                if (at.ActionStatu == type)

                {

                   Object ob = Assembly.GetExecutingAssembly().CreateInstance(type.FullName);

 

                    return ob as IActionManager;

                }

            }

            return null;

 

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            Light light = new Light();

 

            light.HandDown(Statu.On);

 

            light.HandDown(Statu.Off);

 

            light.RemoteDown(Statu.On);

 

            light.RemoteDown(Statu.Off);

        }

    }

}

 

posted @ 2009-03-11 19:26  桑叶舟  阅读(192)  评论(0编辑  收藏  举报