首页 > 其他 > 详细

不知道属于那种设计模式 避免使用if else 语句

时间:2020-07-18 09:40:01      阅读:64      评论:0      收藏:0      [点我收藏+]
#include <iostream>
#include <thread>
#include <chrono>
#include <mutex>
#include <deque>
#include <map>
#include <condition_variable>
using namespace std;
typedef void(*fun)(char *) ;
class regist {
public:
    regist() {};
    void add(std::string key, fun f) {
        m[key] = f;
    };
    fun run(std::string key) {
        return m[key];
    }
private:
    std::map<std::string, fun > m;
};
void a(char *a) {
    cout << "a:" << a << endl;
}
void b(char *a) {
    cout <<"b:"<< a << endl;
}
int main() {
    regist reg;
    reg.add("a",a);
    reg.add("b", a);
    reg.run("b")((char *)"aaaa");
    reg.run("a")((char *)"bbbbb");
    return 0;
}

 

不知道属于那种设计模式 避免使用if else 语句

原文:https://www.cnblogs.com/liuguoyao514257665/p/13334305.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!