zoukankan      html  css  js  c++  java
  • 什么是表驱动

    当if...else等条件调用很多时,将其设计成表驱动的方式可简化代码和提高可维护性。表驱动简单讲(我的理解)就是将条件和约束置入表格中,然后通过表操作来统一执行条件转移操作。如下代码存在这个问题:

    5 太多的if-else而不会用表驱动
    ppro/PrivacyProtection/rule/KSystemEnvirVar.h

    class KSystemEnvirVar
    {
    public:
    // MichaelPeng: 应为静态函数
    CString GetValue(LPCTSTR szVariable)
    {
    TCHAR szFolderPath[MAX_PATH +
    1] = {0};
    // MichaelPeng: if else太多,应做成表驱动

    if (0
    == _tcsicmp(szVariable, _T("%Desktop%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_DESKTOP, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%Internet%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_INTERNET, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%Programs%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_PROGRAMS, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%Controls%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_CONTROLS, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%Printers%"))) {
    http://hi.baidu.com/lewutian
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_PRINTERS, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%Personal%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_PERSONAL, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%Favorites%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_FAVORITES, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%Startup%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_STARTUP, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%Recent%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_RECENT, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%Sendto%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_SENDTO, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%Bitbucket%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_BITBUCKET, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%StartMenu%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_STARTMENU, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%Mydocuments%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_MYDOCUMENTS, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%Mymusic%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_MYMUSIC, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%Myvideo%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_MYVIDEO, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%Desktopdirectory%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_DESKTOPDIRECTORY, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%Drives%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_DRIVES, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%Network%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_NETWORK, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%Nethood%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_NETHOOD, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%Fonts%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_FONTS, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%Templates%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_TEMPLATES, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%CommonStartMenu%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_COMMON_STARTMENU, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%CommonPrograms%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_COMMON_PROGRAMS, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%CommonStartup%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_COMMON_STARTUP, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%LocalAppdate%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_LOCAL_APPDATA, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%CommonAppdata%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_COMMON_APPDATA, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%Windows%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_WINDOWS, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%System32%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_SYSTEM, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%ProgramFilesComm%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_PROGRAM_FILES_COMMON, 0);
    } else
    if (0
    == _tcsicmp(szVariable, _T("%CommonDocuments%"))) {
    ::SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_COMMON_DOCUMENTS, 0);
    }
    else
    {
    CString strVariable(szVariable);
    strVariable.Remove(_T('%'));

    TCHAR* szTemp = _tgetenv(strVariable);
    if (szTemp)
    {
    if (_tcsstr(szTemp, L"~"))
    {
    ::GetLongPathNameW(szTemp, szFolderPath, MAX_PATH);
    }
    else
    {
    _tcsncpy(szFolderPath, szTemp, MAX_PATH);
    }
    szFolderPath[MAX_PATH] =
    0;
    }
    }

    return szFolderPath;
    }

    protected:

    };

    更加详细的说明可以参考这里:

    http://baike.baidu.com/view/3707325.htm 

  • 相关阅读:
    angularjs html事件(ng-click,ng-hide,ng-show)
    《javascript设计模式与开放实践》学习(二)对象方法的借用
    《javascript设计模式与开放实践》学习(二)Function.prototype.bind
    《javascript设计模式与开放实践》学习(一)javascript实现多态2
    《javascript设计模式与开放实践》学习(一)javascript实现多态1
    关于BFC
    css三角形的绘制
    ER图
    如何使用powerDesigner 生成 html文件
    spring aop 会根据实际情况(有无接口)自动选择 两种 动态代理(jdk和cglib)之一
  • 原文地址:https://www.cnblogs.com/seon/p/2105988.html
Copyright © 2011-2022 走看看