如何使用多态性+重载,以便减少IS(类型检查),以提高本方法? [英] how to improve this method using polymorphism+overloading so as to reduce IS (type check)?

查看:125
本文介绍了如何使用多态性+重载,以便减少IS(类型检查),以提高本方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如

BaseClass MyBase()
{
    public int Add(BaseClass next)
    {
        if (this is InheritedA && next is InheritedA)
            return 1;
        else if (this is InheritedA && next is InheritedB)
            return 2;
        else if (this is InheritedB && next is InheritedA)
            return 3;
        else if (this is InheritedB && next is InheritedB)
            return 4;      
     }
}

其中, InheritedA InheritedB 是其继承的类。事实上,有更多的继承类和添加基于其操作数的顺序和类型上返回不同的结果。

where InheritedA, and InheritedB are its inherited classes. In fact, there are more Inherited classes, and the Add returns different results based on the order and types of its operand.

我想重写它使用多态和重载的,但是,它变得相当复杂,我要介绍的helper方法来解决两端的类型。

I am thinking of rewriting it using Polymorphism and overloading, however, it becomes rather complicated, I have to introduce an helper method to resolve the type of either end.

例如

InheritedA myA()
{
    public override int Add(BaseClass next)
    {
        return next.AddTo(this);
    }
}

现在我已经把 AddTo就的BaseClass ,并覆盖它的继承类为好。

Now I have to put AddTo into BaseClass, and override it in inherited class as well.

InheritedA myA()
{
    public override int AddTo(InheritedA next) { return 1; }
    public override int AddTo(InheritedB next) { return 3; }
}

BaseClass myBase()
{
    public abstract int Add(BaseClass next);
    public abstract int AddTo(InheritedA next);
    public abstract int AddTo(InheritedB next);
}

是否有这样做的更好的办法?

Is there a better way of doing it?

推荐答案

您所用的模式被称为的双虚拟调度的。

The pattern you are implementing is called double virtual dispatch.

一个的单个虚拟调度的选择的基础上,调用哪个方法的运行时类型的接收器的和的编译时类型的的论点。这是一种传统的虚拟调度:

A single virtual dispatch chooses which method to call on the basis of the run-time type of the receiver and the compile time type of the arguments. This is a traditional virtual dispatch:

abstract class Animal {}
class Tiger : Animal {}
class Giraffe : Animal {} 
class B
{
    public virtual void M(Tiger x) {}
    public virtual void M(Animal x) {}
}
class D : B
{
    public override void M(Tiger x) {}
    public override void M(Animal x) {}
}
...
B b = whatever;
Animal a = new Tiger();
b.M(a);

哪种方法叫什么名字? B.M(虎) D.M(虎)未选择;我们拒绝他们根据编译时间的类型参数,它是动物。但是,我们选择是否调用 BM(动物) DM(动物)在运行时根据是否任何新B()新D()

Which method is called? B.M(Tiger) and D.M(Tiger) are not chosen; we reject them based on the compile time type of the argument, which is Animal. But we choose whether to call B.M(Animal) or D.M(Animal) at runtime based on whether whatever is new B() or new D().

双虚拟的调度选择要调用的基础上运行时类型的两件事情的哪一种方法。如果C#支持双虚拟调度,它没有,则运行时调度会去 BM(虎) DM(虎)即使参数的编译时类型的动物

Double virtual dispatch chooses which method to call based on the runtime types of two things. If C# supported double virtual dispatch, which it does not, then the runtime dispatch would go to B.M(Tiger) or D.M(Tiger) even though the compile-time type of the argument is Animal.

C#4不支持不过的动态的调度。如果你说

C# 4 does however support dynamic dispatch. If you say

dynamic b = whatever;
dynamic a = new Tiger();
b.M(a);

然后M的分析会做的完全在运行时的使用运行时类型 A 。这是显著慢,但它确实工作。

Then the analysis of M will be done entirely at runtime using the runtime types of b and a. This is significantly slower, but it does work.

另外,如果你想要做的双虚拟调度的并获得尽可能多的分析在编译时完成尽可能那么标准的方式来做到这一点是实施的访问者模式,你可以看一下在互联网上很容易。

Alternatively, if you want to do double virtual dispatch and get as much analysis done at compile time as possible then the standard way to do that is to implement the Visitor Pattern, which you can look up on the internet easily.

这篇关于如何使用多态性+重载,以便减少IS(类型检查),以提高本方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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