为什么首选非虚拟接口? [英] Why is it preferred to have non-virtual interfaces?

查看:141
本文介绍了为什么首选非虚拟接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览 http://www.gotw.ca/publications/mill18。 hbm 由Herb Sutter撰写。作者提到,编写非虚拟接口会将接口规范与实现细节(即内部可自定义行为)分开。

I am going through this article http://www.gotw.ca/publications/mill18.htm by Herb Sutter. The author mentions that writing non-virtual interfaces separates the interface specification from the "implementation details (namely the internally customizable behavior)"

// Example 1: A traditional base class.
//
class Widget
{
public:
  // Each of these functions might optionally be
  // pure virtual, and if so might or might not have
  // an implementation in Widget; see Item 27 in [1].
  //
  virtual int Process( Gadget& );
  virtual bool IsDone();
  // ...
};

上面的例子指定了哪种实现细节(或可自定义的行为)?我对上面的代码有什么问题感到有些困惑,这需要我们有非虚拟接口

What kind of implementation detail (or customizable behavior) does the above example specify? I am slightly confused about what is wrong with the above code, which requires us to have non-virtual interfaces

推荐答案

可自定义的行为它表示由不同的派生类提供的实现,即将从派生的类接口

By customizable behavior it means the implementation provided by different Derived Classes, i.e the classes which will derive from your Interface.

考虑一下:

class IMachine
{
    public:
        int ProcessJob()
        {
            cout << "Processing Job in By-Default way" << endl;
        }
        virtual int ProcessOrder()
        {
            cout << "Processing Order in By-Default way" << endl;
        }
};
class CMachine_A : public IMachine
{
    public:
        int ProcessJob()
        {
            cout << "Processing Job in Machine A's way" << endl;
        }
        int ProcessOrder()
        {
            cout << "Processing Order in Machine A's way" << endl;
        }
};
class CMachine_B : public IMachine
{
    public:
        int ProcessJob()
        {
            cout << "Processing Job in Machine B's way" << endl;
        }
        int ProcessOrder()
        {
            cout << "Processing Order in Machine B's way" << endl;
        }
};

IMachine *pMachine;
CMachine_A oMachineA;
CMachine_B oMachineB;

pMachine = &oMachineA;
pMachine->ProcessJob();
pMachine = &oMachineB;
pMachine->ProcessJob();

Output:
Processing Job in By-Default way
Processing Job in By-Default way

因此,在上面的示例中,即使 pMachine 指向不同的具体实现(读取:派生类),输出也是相同的,无论是选择的实现/派生类。也就是说,机器A和机器B的可自定义行为未生效或未兑现。因此,通过非虚拟 IMachine :: ProcessJob(),接口 IMachine 具有分离/忽略/禁止处理作业的方式,无论机器的类型如何( CMachine_A CMachine_B 使用。

So, in above example even though pMachine points to different concrete implementations (read: derived classes), the output is same irrespective of chosen implementation/derived class. That is, the customizable behavior of Machine A and Machine B is not coming in-effect or not honored. So, by having non virtual IMachine::ProcessJob(), the interface IMachine has separated/ignored/suppressed the way in which the Job will get processed irrespective of the type of Machine (CMachine_A or CMachine_B) which is used.

现在考虑一下:

IMachine *pMachine;
CMachine_A oMachineA;
CMachine_B oMachineB;

pMachine = &oMachineA;
pMachine->ProcessOrder();
pMachine = &oMachineB;
pMachine->ProcessOrder();

Output:
Processing Order in Machine A's way
Processing Order in Machine B's way

这里,当 pMachine 指向不同的具体实现(读取:派生类)时,输出是根据所选的实现/派生类。也就是说,机器A和机器B的可自定义行为即将生效或兑现。因此,通过虚拟 IMachine :: ProcessOrder(),接口 IMachine 保留根据机器类型( CMachine_A CMachine_B )处理订单将被处理的选项/方式使用。

Here, when pMachine points to different concrete implementations (read: derived classes), the output is according to the chosen implementation/derived class. That is, the customizable behavior of Machine A and Machine B is coming in-effect or honored. So, by having virtual IMachine::ProcessOrder(), the interface IMachine has kept the option/way open in which the Order will get processed depending upon the type of Machine (CMachine_A or CMachine_B) which is used.

简而言之,因为接口 IMachine 保留了 ProcessOrder as virtual 因此,不同的实现/派生类可以为函数<$ c $提供可自定义行为 c> ProcessOrder 。

In short, since the interface IMachine has kept the ProcessOrder as virtual therefore different implementation/derived class can provide customizable behavior for the function ProcessOrder.

这篇关于为什么首选非虚拟接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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