简体   繁体   English

重构类

[英]Refactoring a class

I have two almost identical classes, in fact every member function is identical, every member is identical, every member function does exactly the same thing. 我有两个几乎完全相同的类,实际上每个成员函数都相同,每个成员都相同,每个成员函数都做完全相同的事情。 The only difference between those classes is the way I can define variable of their type: 这些类之间的唯一区别是我可以定义其类型的变量的方式:

AllocFactorScientific<102,-2> scientific;
AllocFactorLinear<1.2> linear;  

Here are headers for them: 这是它们的标题:

template<double&& Factor>
struct AllocFactorLinear;

template<short Mantissa, short Exponent, short Base = 10>
struct AllocFactorScientific

My question is how can I refactor those functions out of those classes that would allow me to have just one set of functions and not two sets of identical functions. 我的问题是如何从那些类中重构那些函数,使我只能拥有一组函数,而不能拥有两组相同的函数。

Extract all the common behavior in a third class (I'm omitting the template arguments in my answer for the sake of clarity) : 提取第三类中的所有常见行为(为了清楚起见,我在答案中省略了模板参数):

class CommonImpl
{
public:
  void doSomething() {/* ... */ }
};

I then see two choices (which are, from my point of view at least, pretty much equivalent) : 然后,我看到两个选择(至少,从我的观点来看,它们几乎是等效的):

  • Make AllocFactorLinear and AllocFactorScientific inherit privately from this class, and bring the member functions you wish to expose in scope with a using directives : 使AllocFactorLinearAllocFactorScientific 私自从此类继承,并using指令将要公开的成员函数带入范围:

     class AllocFactorLinear : CommonImpl { public: using CommonImpl::doSomething; }; 
  • Aggregate the implementation class in AllocFactorLinear and AllocFactorScientific and forward all the calls to the private implementation : 聚合AllocFactorLinearAllocFactorScientific的实现类, AllocFactorLinear所有调用转发给私有实现:

     class AllocFactorLinear { public: void doSomething() { impl_.doSomething(); } private: CommonImpl impl_; }; 

I would personally go for the first solution. 我个人会寻求第一个解决方案。

也许尝试使用此函数创建一些基类,并使用有关此基类继承的模板来完成这两个类。

Why do you use a templated class for this? 为什么为此使用模板化类? Couldnt you use a untemplated class with 2 different constructors? 您能否将未模板化的类与2个不同的构造函数一起使用?

I think it would be something like: 我认为应该是这样的:

template <typename Type>
struct AllocFactor {...};

and then you can have Type , for example: 然后可以输入Type ,例如:

template <double&& Factor>
struct LinearConfig
{
    static double value() { return Factor;}
};

and: 和:

template <short Mantissa, short Exponent, short Base = 10>
struct FactorScientificConfig
{
    static double value() 
    {
       return some_expression_to_get_factor;
    }
};

You can create the AllocFactor using AllocFactor<LinearConfig<1.2>> and the corresponding with the FactorScientificConfig . 您可以创建AllocFactor使用AllocFactor<LinearConfig<1.2>> ,并与相应的FactorScientificConfig Then you can use the static member function to return the value calculated for both the classes, so that AllocFactor<T> can use T::value() as the value reported by the config class. 然后,您可以使用静态成员函数返回为两个类计算的值,以便AllocFactor<T>可以使用T::value()作为config类报告的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM