customizable behavior 表示不同Derived Classes 提供的实现,即派生自您的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 的 customizable behavior 没有生效或没有兑现。因此,通过 非虚拟 IMachine::ProcessJob(),接口 IMachine 已经分离/忽略/抑制了处理作业的方式,而与机器类型无关(CMachine_A 或 @987654330 @) 使用。
现在考虑一下:
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 的 customizable behavior 即将生效或兑现。因此,通过 virtual IMachine::ProcessOrder(),界面 IMachine 保持打开选项/方式,根据机器类型(CMachine_A 或 CMachine_B)处理订单使用哪个。
简而言之,由于接口IMachine 将ProcessOrder 保持为virtual,因此不同的实现/派生类可以为函数ProcessOrder 提供customizable behavior。