动态绑定或开关/案例? [英] dynamical binding or switch/case?

查看:28
本文介绍了动态绑定或开关/案例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这样的场景:
我有不同的对象执行与各自的 func() 实现类似的操作.
func_manager() 根据不同的对象调用 func() 有两种解决方案

A scene like this:
I've different of objects do the similar operation as respective func() implements.
There're 2 kinds of solution for func_manager() to call func() according to different objects

解决方案1:使用c++中指定的虚函数字符.func_manager 的工作方式根据不同的对象点传入不同.

Solution 1: Use virtual function character specified in c++. func_manager works differently accroding to different object point pass in.

class Object{
  virtual void func() = 0;
}
class Object_A : public Object{
  void func() {};
}
class Object_B : public Object{
  void func() {};
}
void func_manager(Object* a)
{
   a->func();
}

解决方案 2:使用普通开关/外壳.func_manager 的工作方式因传入的类型不同而不同

Solution 2: Use plain switch/case. func_manager works differently accroding to different type pass in

typedef enum _type_t
{
  TYPE_A,
  TYPE_B
}type_t;

void func_by_a()
{
// do as func() in Object_A
}
void func_by_b()
{
// do as func() in Object_A
}
void func_manager(type_t type)
{
    switch(type){
        case TYPE_A:
            func_by_a();
            break;
        case TYPE_B:
            func_by_b();
        default:
            break;
    }
}

我的问题是 2:
1.从DESIGN PATTERN的角度来看,哪个更好?
2.从RUNTIME EFFCIENCE的角度来看,哪个更好?特别是随着对象种类的增加,总共可能达到 10-15 个,哪一个的开销超过另一个?不知道switch/case内部是怎么实现的,就一堆if/else?

My Question are 2:
1. at the view point of DESIGN PATTERN, which one is better?
2. at the view point of RUNTIME EFFCIENCE, which one is better? Especailly as the kinds of Object increases, may be up to 10-15 total, which one's overhead oversteps the other? I don't know how switch/case implements innerly, just a bunch of if/else?

非常感谢!

推荐答案

从DESIGN PATTERN的角度来看,哪个更好?

from the view point of DESIGN PATTERN, which one is better?

使用多态性(解决方案 1)更好.
只有一个数据点:假设您有一个围绕两者之一构建的庞大系统,然后突然需要添加另一种类型.使用解决方案一,您添加一个派生类,确保在需要的地方对其进行实例化,然后就完成了.使用解决方案 2,您有 成千上万的 switch 语句 遍布整个系统,并且或多或少不可能保证您找到了所有的地方您必须为新类型修改它们.

Using polymorphism (Solution 1) is better.
Just one data point: Imagine you have a huge system built around either of the two and then suddenly comes the requirement to add another type. With solution one, you add one derived class, make sure it's instantiated where required, and you're done. With solution 2 you have thousands of switch statements smeared all over the system and it is more or less impossible to guarantee you found all the places where you have to modify them for the new type.

从 RUNTIME EFFCIENCE 的角度来看,哪个更好?特别是作为对象的种类

from the view point of RUNTIME EFFCIENCE, which one is better? Especailly as the kinds of Object

这很难说.
我记得 Stanley Lippmann 的 C++ 对象模型内部 中的一个脚注,他说研究表明虚函数在类型切换方面可能有一点优势.然而,我很难引用章节和经文,而且,IIRC,优势似乎不足以让决定依赖它.

That's hard to say.
I remember a footnote in Stanley Lippmann's Inside the C++ Object Model, where he says that studies have shown that virtual functions might have a small advantage against switches over types. I would be hard-pressed, however, to cite chapter and verse, and, IIRC, the advantage didn't seem big enough to make the decision dependent on it.

这篇关于动态绑定或开关/案例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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