应该将NVI成语用于简单的接口类吗? [英] Should the NVI idiom be used for simple interface classes?

查看:134
本文介绍了应该将NVI成语用于简单的接口类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该使用什么?

struct IFilterTreeNode
{
    virtual unsigned int GetEasiestProveRank() const = 0;
    virtual unsigned int GetEasiestDisproveRank() const = 0;
    virtual unsigned int GetEasiestProveNumber() const = 0;
    virtual unsigned int GetEasiestDisproveNumber() const = 0;
    virtual std::vector<IFilterTreeNode *> GetChildren() const = 0;
    virtual bool AttemptToProve() = 0;
    virtual bool AttemptToDisprove() = 0;
    virtual ~IFilterTreeNode() {};
};

或:

class IFilterTreeNode
{
    virtual unsigned int GetEasiestProveRankImpl() const = 0;
    virtual unsigned int GetEasiestDisproveRankImpl() const = 0;
    virtual unsigned int GetEasiestProveNumberImpl() const = 0;
    virtual unsigned int GetEasiestDisproveNumberImpl() const = 0;
    virtual std::vector<IFilterTreeNode *> GetChildrenImpl() const = 0;
    virtual bool AttemptToProveImpl() = 0;
    virtual bool AttemptToDisproveImpl() = 0;
public:
    unsigned int GetEasiestProveRank() const
    {
        return GetEasiestProveRankImpl();
    }
    unsigned int GetEasiestDisproveRank() const
    {
        return GetEasiestDisproveRankImpl();
    }
    unsigned int GetEasiestProveNumber() const
    {
        return GetEasiestProveNumberImpl();
    }
    unsigned int GetEasiestDisproveNumber() const
    {
        return GetEasiestDisproveNumberImpl();
    }
    std::vector<IFilterTreeNode *> GetChildren() const
    {
        return GetChildrenImpl();
    }
    bool AttemptToProve()
    {
        return AttemptToProveImpl();
    }
    bool AttemptToDisprove()
    {
        return AttemptToDisproveImpl();
    }
    virtual ~IFilterTreeNode() {};
};


推荐答案

我很懒,第一选择,所以这是一个我会去。当事情开始变得更复杂时,我立即切换到第二个选择。但是像我说的,我很懒。 : - )

I'm lazy, and most of our codebase uses the first choice, so that's the one I'd go with. I'd switch to the second choice immediately when things start getting more complex. But like I said, I'm lazy. :-)

第二种选择肯定更加面向未来,更灵活。好的程序员使类容易使用。让他们容易写是次要的。

The second choice is definitely more future-proof and more flexible. Good programmers make classes easy to use. Making them easy to write is secondary.

这篇关于应该将NVI成语用于简单的接口类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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