委派模板成员功能的良好设计 [英] Good design for delegating template-member functionality

查看:85
本文介绍了委派模板成员功能的良好设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下情况下,我无法找到简单而优雅的设计。类 Worker 使用模板类 Helper 进行一些工作。在简单的情况下,它看起来像这样:

I am having trouble in finding simple and elegant design for the following scenario. Class Worker uses template class Helper to do some work. In the simple scenario it looks like this:

template<typename T>
class Helper {
  public:
    void Help(T data) : m_data(data) {} //NOTICE: Copy
    T const& Data() const { return m_data; }

    T m_data;
}

class SimpleWorker {
  public:
    SimpleWorker() : m_helper(SimpleData()) {} // non-temp used in reality
    void DoWork()
    {
        m_helper.help();
    }        

    Helper<SimpleData> m_helper;
}

当模板参数更复杂且属于工人的相同业务领域。工作人员需要使用帮助程序,但它也需要在该帮助程序甚至不知道的数据对象上调用某些方法(在此设计中)。像这样的东西:

Things get complicated for me when the template argument is more complex and is of the same business domain of the worker. The worker needs to use the helper but it would also need to call some methods on the data object that the helper doesn't even know about (in this design). Something like:

template<typename T>
class Helper {
  public:
    Helper(T data) : m_data(data) {} //NOTICE: Copy
    T const& Data() const { return m_data; }

    T m_data;
}

class ComplexWorker {
  public:
    ComplexWorker() : m_helper(ComplexData()) {} // non-temp used in reality

    void DoWork()
    {
        m_helper.help();
        m_helper.GetData().DoSomethingComplexNotConst(); // <-------------
    }    

    Helper<ComplexData> m_helper;    
}

明显的问题是我不能在<$上调用not const function c $ c> Data()结果。使 Data()为非常量似乎是个坏主意,因为 Helper 也用于不同的上下文。我的目标是找到一种优雅的方法来使用 ComplexWorker
中的 ComplexData 成员函数来更改 ComplexData ,以便 Helper 可以继续使用更改后的数据。

The obvious problem is that I can't call not const function on Data() result. Making Data() non-const seems like a bad idea as Helper is used in different contexts as well. My goal is to find an elegant way to alter ComplexData using its member functions from ComplexWorker The ComplexData needs to be altered in the first place so that Helper can continue working with the altered data.

编辑::更改了 Helper 的构造,以复制提供的数据以更好地类似于实际流程

Changed Helper construction to copy the provided data to better resemble actual flow

推荐答案

我认为最好让 Helper 仅具有静态功能,而不要保持状态(如您在您自己的代码中的 ComplexWorker 中创建临时 ComplexData())。通过引用或const引用传递数据,具体取决于您是否需要进行修改。

I think it's best to let Helper have only static functions, not maintaing state (as you create temporary ComplexData() in ComplexWorker in your own code). Pass the data by reference or const-reference depending on whether you need to modify or not.

// primary template
template<typename T>
class Helper {
public:
    static void help(T const& data) const {} // non-modifying
};

// specialization for ComplexData
template<>
class Helper<ComplexData> {
public:
    static void help(ComplexData const& data) const { } // non-modifying

    static void DoSomethingComplexNotConst(ComplexData& data) // modifying
    {
         // your implementation here
    }
};

class ComplexWorker {
public: 
    ComplexWorker() : m_data(ComplexData()) {} // create new data

    void DoWork()
    {
        Helper<ComplexData>::help(m_data);
        Helper<ComplexData>::DoSomethingComplexNotConst(m_data); // <--- now no problem
    }

   private:
       ComplexData m_data;         
};

请注意,我为模板专业化 > ComplexData 。 help()中有一些重复的代码,但是您可以轻松地将其提取到常见的非成员帮助器函数中。

Note that I made a template specialization for the ComplexData. There is some code duplication in help() but you can easily extract this into a common non-member helper function.

这篇关于委派模板成员功能的良好设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆