什么是表驱动

当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 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值