想想我们在遇到多语句分支时是不是首先想到的是 switc case 和 if else if ...
这2种方式在编码方面确实简单少,但是当分支达到一定数量后,特别是分支内部有嵌套大段代码或者再嵌套分支,
代码会显得异常臃肿,十分难以维护,对于if else if 语句过多的分支带来过多的判定句,势必会影响效率。
3种替代方法简述:
1.使用map,需要构建树和节点,比数组的方式消耗更多的内存,查询时间复杂度为Log(N),但扩展起来方便。
2.使用数组,查询直接索引定位, 一般来讲我们是连续的初始化数组,也就意味索引(type_func)到函数的映射要连续,
所以使用数组索引在扩展上来讲:例如增删元素是稍微麻烦点的。
3.使用C++的特性---抽象继承来实现,本文只讲前2种的使用,这种方式以后再补充。
我比较喜欢用代码结合实际来讲解,下面我将以一段事例代码来讲解如何使用这几种映射:
// 动物会一些动作
enum type_func
{type_begin = -1,type_eat,type_sleep,type_walk,type_run,type_smile,type_cry,type_jump,type_max_size,
};class CAnimal
{
public:typedef int (CAnimal::*ptr_func)(bool);protected:static map<type_func,ptr_func> s_map; static ptr_func s_array[type_max_size]; public:CAnimal(){memset(s_array,0,sizeof(s_array));Init(); }// 需要映射函数的返回值 和 参数必须 统一int eat (bool= true) { return printf("eatn") ,1; }int sleep (bool= true) { return printf("sleepn"),1; }int walk (bool= true) { return printf("walkn") ,1; }int run (bool= true) { return printf("runn") ,1; }int smile (bool= true) { return printf("smilen"),1; }int cry (bool= true) { return printf("cryn") ,1; }int jump (bool= true) { return printf("jumpn") ,1; }// 初始化void Init (){s_map[type_eat] = &CAnimal::eat;s_map[type_sleep] = &CAnimal::sleep;s_map[type_walk] = &CAnimal::walk;s_map[type_run] = &CAnimal::run;s_map[type_smile] = &CAnimal::smile;s_map[type_cry] = &CAnimal::cry;s_map[type_jump] = &CAnimal::jump;s_array[type_eat] = &CAnimal::eat;s_array[type_sleep] = &CAnimal::sleep;s_array[type_walk] = &CAnimal::walk;s_array[type_run] = &CAnimal::run;s_array[type_smile] = &CAnimal::smile;s_array[type_cry] = &CAnimal::cry;s_array[type_jump] = &CAnimal::jump;}// 一般做法是switc case 或者 if else... // 其实这里看起来还不算糟糕,一方面这里我把每个模块内容都封装到相应函数了// 分支内部才会看起来相对简洁,实际编码中可能就不是你现在所看到的方式。void Process (type_func type){switch (type){case type_eat: eat(); break;case type_sleep: sleep(); break;case type_walk: walk(); break;case type_run: run(); break;case type_smile: smile(); break;case type_cry: cry(); break;case type_jump: jump(); break;}}// 很熟悉的感觉吧! :)void Process2(type_func type){if (type_eat == type){eat();}else if (type_sleep == type){sleep();}else if (type_walk == type){walk();}else if (type_run == type){run();}else if (type_smile == type){smile();}else if (type_cry == type){cry();}else if (type_jump == type){jump();}}// 使用map 映射void ProcessByUseMap(int key, bool val){map<type_func,ptr_func>::iterator it = s_map.find((type_func)key);if (it != s_map.end()){ptr_func pFun = it->second;if (pFun) (this->*pFun)(val);}}// 使用数组 映射void ProcessByUseArray(int key, bool val){// 数组if (type_begin < key && type_max_size > key){ptr_func pFun = s_array[key];if (pFun) (this->*pFun)(val);}}// 使用map 映射int operator[] (int key){map<type_func,ptr_func>::iterator it = s_map.find((type_func)key);if (it != s_map.end()){ptr_func pFun = it->second;if (pFun) return (this->*pFun)(false);}return NULL;}// 使用数组 映射int operator() (int key,bool val){if (type_begin < key && type_max_size > key){ptr_func pFun = s_array[key];if (pFun) return (this->*pFun)(val);}return NULL;}};map<type_func, CAnimal::ptr_func> CAnimal::s_map;
CAnimal::ptr_func CAnimal::s_array[type_max_size];//
// 非成员函数
void func_eat(int = 0) { }
void func_run(int = 0) { }
void func_walk(int =0) { }
void func_cry(int = 0) { }typedef void (*ptrFun)(int);
map<type_func,ptrFun> g_map;
ptrFun g_array[type_max_size];int _tmain(int argc, _TCHAR* argv[])
{//// 为了便于说明,下面代码不做安全检查// 非成员函数映射2种用法// initg_map[type_eat] = func_eat;g_map[type_run] = func_run;g_map[type_walk] = func_walk;g_map[type_cry] = func_cry;g_array[type_eat] = func_eat;g_array[type_run] = func_run;g_array[type_walk] = func_walk;g_array[type_cry] = func_cry;// usingg_map[type_eat](1);g_map[type_run](2);g_map[type_walk](3);g_map[type_cry](4);g_array[type_eat](1);g_array[type_run](2);g_array[type_walk](3);g_array[type_cry](4);//// 成员函数映射使用CAnimal Dog;Dog.Process(type_eat);Dog.ProcessByUseMap(type_run,true);Dog.ProcessByUseArray(type_cry,false);Dog[type_walk];Dog(type_sleep,true);Dog(type_run,false);return 1;
}