zoukankan      html  css  js  c++  java
  • 当使用Switch时 case太多,用委托代替

     switch (_mediaState)
                {
                    case State.Run:
                      AAA();
                        break;
                    case State.Stop:
                       BBB();
                        break;
                    case State.Forward:
                        CCC();
                        break;
                    case State.Rewind:
                        DDD();
                        break;
                }             
    public delegate void RelaceSwitch();
    private  static Dictionary<State,RelaceSwitch> ReplaceSwitch=new Dictionary<State, RelaceSwitch>();
    
            public static void MethodRun()
            {
                AAA();
            }
            public static void MethodStop()
            {
                BBB();
            }
            public static void MethodForward()
            {
                CCC();
            }
            public static void MethodRewind()
            {
                DDD();
            }
    
    
            private static void Adddic()
            {
                ReplaceSwitch.Add(State.Run, MethodRun);//ReplaceSwitch[State.Run]=MethodRun;
                ReplaceSwitch.Add(State.Stop, MethodStop);
                ReplaceSwitch.Add(State.Forward, MethodForward);
                ReplaceSwitch.Add(State.Rewind, MethodRewind);
            }
    
            private static void Exec(State state, RelaceSwitch action)
            {
                action.Invoke();
            }

    调用

    去掉switch

    首先Adddic();

    之后Exec(_mediaState, ReplaceSwitch[_mediaState]);

  • 相关阅读:
    SPF(poj 1523) 割点入门
    从一个例子讲解拷贝构造函数与return
    还是引用
    引用的实质
    const
    三目运算符
    关于C语言链表的学习
    CS2013调试DLL
    fread与fwrite的自我理解
    可见字符与不可见字符
  • 原文地址:https://www.cnblogs.com/dzh1990/p/8276053.html
Copyright © 2011-2022 走看看