Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 这是否意味着;“语境模式”;看起来还好吗?_C++_Design Patterns - Fatal编程技术网

C++ 这是否意味着;“语境模式”;看起来还好吗?

C++ 这是否意味着;“语境模式”;看起来还好吗?,c++,design-patterns,C++,Design Patterns,我有多个可能存在于一个数组中的处理单元,每个处理单元都有自己的参数。我想在建议将上下文模式作为一种模式之后,使用上下文模式来传达每个处理单元的参数。但是,我无法找到简单的C++模式的在线例子。我在下面做了一个简化的实现,供您检查。代码运行和编译都很好,但是我是否正确地实现了模式?任何和所有关于样式改进的建议都是非常受欢迎的 #include <iostream> #include <sstream> #include <map> class cParams

我有多个可能存在于一个数组中的处理单元,每个处理单元都有自己的参数。我想在建议将上下文模式作为一种模式之后,使用上下文模式来传达每个处理单元的参数。但是,我无法找到简单的C++模式的在线例子。我在下面做了一个简化的实现,供您检查。代码运行和编译都很好,但是我是否正确地实现了模式?任何和所有关于样式改进的建议都是非常受欢迎的

#include <iostream>
#include <sstream>

#include <map>

class cParamsContext
{      
     typedef std::map<std::string, float> myMap_t; //Make the return type of getter less wordy
     myMap_t paramMap;

public:              
    cParamsContext()
    {
        paramMap["a0"] = 1.f;
        paramMap["a1"] = 2.f;
    }

    myMap_t const& getMap() const {return paramMap;} //Return read-only alias   
};

class cProcessUnit
{
    float parameter;
    int id;

public:
    cProcessUnit(cParamsContext &contextObj, int id_) : parameter (0.f), id(id_)
    {
        std::stringstream idStream;
        idStream << id;

        std::string key = std::string( "a" + idStream.str() );

        if(contextObj.getMap().find( key ) != contextObj.getMap().end())        
            parameter = contextObj.getMap().find( key )->second; // https://stackoverflow.com/questions/10402354/using-overloaded-operator-via-an-accessor-function#10402452                 
    }

    float getParam() {return parameter;}
};


int main(int argc, char *argv[])
{
    cParamsContext contextObj;


    for (int nn=0; nn<3; nn++)
    {
        cProcessUnit temp(contextObj, nn);
        std::cout << "Processing unit "  << nn << " param = " << temp.getParam() << std::endl;
    }
}

这看起来确实是一个有效的实现。它通过你的测试了吗?我没有以这种特殊的方式使用上下文模式的经验,但在我看来确实不错


至于更新值,我目前在一个分配给我的项目中做了一些非常类似的事情,我正在使用。在这种情况下,
cParamsContext
将是可见的。我正在使用观察者模式的实现。到目前为止,它为我的任务创造了奇迹。

看起来它可以正常工作,但这里有一些建议:

  • 考虑使用性能更好的哈希映射/表。我有一个你可以看的。映射在性能上很好,但根据数据集的不同,哈希表的性能可能会更高

  • 考虑将值模板化,或者至少通过为每个类型和相应的getter/setter创建一个映射来允许不同的类型。现在,使用的参数只能是float,但是如果将来需要不同的参数呢


  • 这应该继续进行代码检查。设置cParamsContextobservable非常好+1感谢您的建议,但请详细说明(1)std::map有什么问题,(2)我正在模板化哪些值,这会给我带来什么好处?请说清楚。
    Processing unit 0 param = 1
    Processing unit 1 param = 2
    Processing unit 2 param = 0