Use tips the sub-function modules do not pollute the main frame

  Use C ++ development in doing message distribution, we often encounter such a scenario, we designed a separate processing class or handler for each message, and use a map to maintain such a correspondence relationship, when the message we traversing the mapping table to find the corresponding handlers, and further process the message.

  We may have encountered this design

#include"case1.h"
#include"case2.h"
#include"case3.h"
#include"case4.h"
#include"case5.h"

void HandleManage::reghandle(){
     _mhandles["cmd_1"] = new case1_handle();
     _mhandles["cmd_2"] = new case2_handle();
     _mhandles["cmd_3"] = new case3_handle();
     _mhandles["cmd_4"] = new case4_handle();
     _mhandles["cmd_5"] = new case5_handle();
}

 Designed to meet our requirements, but will bring the two issues

   1, with the iterative development, there will be a large number of new case came in, so regHandle will become very long, but at the same time have a lot of case is no longer useful and takes up a mapping table while also reducing Find the message map effectiveness

   2, when we do not want to use the old case when the registration module need to manually remove, maintenance is more difficult

In order to solve these two problems I intend to re-design handle management module:

HandleManage.h

#pragma once
#include <map>
#include <string>
#include "IHandle.h"
using namespace std;
class HandleManager
{
public:
    static HandleManager* instance() {
        if (_pInstance == NULL) {
            _pInstance = new HandleManager();
        }
        return _pInstance;
    }

    void regHandle(const string& cmd, IHandle* handle);
    void unregHandle(const string& cmd);
    IHandle * getHandle(const string& cmd);
    void onHandle(const string& cmd);
private:
    map<string, IHandle*> _map_handle;
    HandleManager(){}
    static HandleManager* _pInstance;
};

HandleManage.cpp

#include "HandleManager.h"

HandleManager* HandleManager::_pInstance = NULL;

void HandleManager::regHandle(const string& cmd, IHandle* handle) {
    _map_handle[cmd] = handle;
}

void HandleManager::unregHandle(const string& cmd) {
    if (_map_handle.find(cmd) != _map_handle.end()) {
        _map_handle.erase(cmd);
    }
}

IHandle * HandleManager::getHandle(const string& cmd) {
    if (_map_handle.find(cmd) != _map_handle.end()) {
        return _map_handle[cmd];
    }
    return NULL;
}

void HandleManager::onHandle(const string& cmd) {
    IHandle * pHandle = getHandle(cmd);
    if (NULL != pHandle) {
        pHandle->handle(cmd);
    }
}

Unified interface IHandle.h message processing

#pragma once
#include<string>
using namespace std;
class IHandle
{
public:
    virtual void handle(const string& cmd) = 0;
};

When we develop a case of every-day build a unique folder (f easy to add compiler path makefile)

With a case as an example:

case_1.h

#pragma once
#include <string>
#include "../manager/HandleManager.h"
using namespace std;
const static string strCmd = "case_1";
class Case_1:public IHandle,public IConfig
{
public:
    static Case_1* instance() {
        if (_pInstance == NULL) {
            _pInstance = new Case_1();
        }

        if (_pInstance != NULL) {
            HandleManager::instance()->regHandle(strCmd, _pInstance);

        }
        return _pInstance;
    }
    void handle(const string& cmd);
private:
    static Case_1* _pInstance;
    Case_1(){}
};

static Case_1 * spInstance = Case_1::instance();

case_1.cpp

#include "Case_1.h"
#include <iostream>

Case_1* Case_1::_pInstance = NULL;

void Case_1::handle(const string& cmd) {
    cout <<"handle:"<< cmd << endl;
}

Test code is as follows:

#include "test/manager/HandleManager.h"
using namespace std;

int main(){

    HandleManager::instance()->onHandle("case_1");
    HandleManager::instance()->onHandle("case_2");

    system("pause");
}

  Testing can be seen in both the inlet and handleManager no code on the particular case, but in each case the function module is injected into the process in the message map, while taking advantage of the characteristics of the external variables, it can be called at the beginning of operation

       When a new case fit into the demands of the case expired or old, we only need to modify the Makefile will be able to ensure that the final process of code contains only valid case

 

Guess you like

Origin www.cnblogs.com/wanzaixiaoxin/p/11862752.html