基于类型的最佳方式切换行为 [英] Best way to switch behavior based on type

查看:116
本文介绍了基于类型的最佳方式切换行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/298976/c-sharp-is-there-a-better-alternative-than-this-to-switch-on-type\">C# - 是否有比这更好的替代品上式开关'

考虑经典的:

class Widget { }
class RedWidget : Widget { }
class BlueWidget : Widget { }

在大多数情况下,在我的UI,我可以把所有的小部件 S也是一样的。不过,也有细微的差别,这是我需要如果开关通过

For the most part, in my UI, I can treat all Widgets the same. However, there are minor differences, which I need to if or switch through.

可能的方法:

枚举指示灯 - 由构造方法设置

Enum Indicator - set by constructor

enum WidgetVariety { Red, Blue }

class Widget {
    public WidgetVariety Variety { get; protected set; }
}

class RedWidget : Widget {
    public RedWidget() {
        Variety = Red;
    }
}

// Likewise for BlueWidget...

switch (mywidget.Variety) {
case WidgetVariety.Red:
    // Red specific GUI stuff

case WidgetVariety.Blue:
    // Blue specific GUI stuff
}

使用

Use is

Widget w = ...;
if (w is RedWidget) {
    (RedWidget)w ...
}
else if (w is BlueWidget) {
    (BlueWidget)w ...
}

我已经使出了这样做的原因是1)大多数code的已经有点这样写的,但更恶心。 2)90%code是相同的 - 在GridView基本上只是一列需要根据不同的类型进行不同的处理。

The reason I've resorted to this is 1) Most of the code is already somewhat written this way, but much uglier. 2) 90% of the code is identical - basically just one column in a GridView needs to be handled differently depending on the type.

哪些建议? (或任何人有更好的解决方案吗?)

Which would you recommend? (Or anyone have a better solution?)

修改我知道我可能会建议访问者模式,但只是看起来复杂,在这种情况下稀疏的,细微的差别。

Edit I know I'll probably be recommended to the Visitor Pattern, but that simply seems to complicated for sparse, minor differences in this case.

编辑2
所以我有一个很难整理出一个特别的区别是本栏认为是两种不同的。在一个案例中,它会检索一个布尔价值,并分配给该网格单元。在其他情况下,它得到一个字符串值。

Edit 2 So one particular difference I was having a hard time sorting out is this column that is different between the two types. In one case, it retrieves a bool value, and assigns that to the grid cell. In the other case, it gets a string value.

我在这种情况下想,这应该是显而易见的,我可以定义:

I suppose in this case, it should be obvious that I could define:

public object virtual GetColumn4Data();

public override GetColumn4Data() { return m_boolval; }

public override GetColumn4Data() { return m_mystring; }

这对我来说感觉不对起初,由于使用了对象的。然而,是的,我在小区分配给该属性的类型,所以当然,这是有道理的!

This felt wrong to me initially, due to the use of object. However, that is the type of the property that I am assigning to in the cell, so of course this makes sense!

太长办公室今天看来...

Too long at the office today it seems...

推荐答案

还有另一种可能性。使用虚拟调度:

There's another possibility. Use virtual dispatch:

class Widget
{
    public virtual void GuiStuff() { }
} 
class RedWidget : Widget
{
    public override void GuiStuff()
    {
        //... red-specific GUI stuff
        base.GuiStuff();
    }
} 
class BlueWidget : Widget
{
    public override void GuiStuff()
    {
        //... blue-specific GUI stuff
        base.GuiStuff();
    }
} 

这篇关于基于类型的最佳方式切换行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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