简体   繁体   English

在这种情况下如何使用委托/泛型?

[英]How to use delegates/ generics in this case?

I have 2 enums enum1, enum2. 我有2个枚举enum1,enum2。

I have two functions : 我有两个功能:

bool func1(int param1 , enum1 type)
bool func1(int param1 , enum2 type)

Both functions have exactly similar structure. 这两个功能具有完全相似的结构。 Only difference in all conditions is: func1 has enum1.str1 is used in all comparisons and func2 has enum2.str2 is used in all comparisons 在所有条件下唯一的区别是:func1具有enum1.str1用于所有比较,func2具有enum2.str2用于所有比较

Since both functions are similar, I want to combine the two.. What is the best way to do it? 由于这两个功能相似,所以我想将两者结合起来。最好的方法是什么?

func1(int param1, enum1 type)
{

      if (type.Equals(enum1.abc))
      {
        // do something
      }
      else if(type.Equals(enum1.def))
      {
    // do something
      }

}


func2(int param1, enum2 type)
{

      if (type.Equals(enum2.abcd))
      {
        // do something
      }
      else if(type.Equals(enum2.defg))
      {
    // do something
      }

}

There are two ways to handle this that are simple and the decision to do this depends on the compatibility of enum1 and enum2 . 有两种简单的处理方法,而执行此操作的决定取决于enum1enum2的兼容性。

If they are 100% compatible, then you can imagine this: 如果它们是100%兼容的,那么您可以想象一下:

bool Func1(int param1, enum1 type)
{
     switch (type) {
         case enum1.abc:
         // ...
         break
         // ...
     }
}

bool Func2(int param1, enum2 type) { Func1(param1, ToEnum1(type)) }

private static enum1 ToEnum1(enum2 type)
{
    switch(type)
    {
        case enum2.abc: return enum1.abc;
        // ...
        default: throw new NotImplementedException("missed a case!");
    }
}

Now, why am I using a function with a switch to convert instead of casting? 现在,为什么我要使用带有开关的函数进行转换而不是强制转换? The answer is to catch the bug when you have change the layout of one enum and not the other - that's bad. 答案是在更改一个枚举而不更改另一个枚举的布局时捕获该错误-这很糟糕。

If the types aren't compatible, consider that you could define a 3rd enum which consists of all the overlapping values plus all the non-overlapping values - you're essentially creating a proper union of the two enumerated types. 如果类型不兼容,请考虑定义一个第三个枚举,该枚举由所有重叠值加上所有非重叠值组成-本质上是在创建两个枚举类型的正确并集。 Convert to that write Func1 based on the third enum. 根据第三个枚举转换为该写Func1。 You can (and probably should) make the third type private to the class that's doing the work. 您可以(可能应该)将第三种类型专用于正在工作的类。

In essence they are essentially the same solution, it's just that in the first case one of the enumerated types is already the same as the third type. 从本质上讲,它们本质上是相同的解决方案,只是在第一种情况下,一种枚举类型已经与第三种相同。

If the underlying enum values match: 如果基础枚举值匹配:

bool Func1(int param1, enum1 type)
{
    return Func2(param1, (enum2)type);
}

If they are different: 如果它们不同:

bool Func1(int param1, enum1 type)
{
    switch (type)
    {
        case enum1.abc: return Func2(param1, enum2.abc);
        case enum1.def: return Func2(param1, enum2.def);
    }
}

It doesn't sound like generics is necessarily the best solution here. 听起来泛型不一定是这里的最佳解决方案。 If you're primarily interested in reducing code reuse, you might try something like this instead: 如果您主要对减少代码重用感兴趣,则可以尝试使用以下方法:

func1(int param1, enum1 type)
{
    if (type.Equals(enum1.abc))
    {
        doSomethingAbc(param1);
    }
    else if(type.Equals(enum1.def))
    {
        doSomethingDef(param1);
    }
}


func2(int param1, enum2 type)
{
    if (type.Equals(enum2.abc))
    {
        doSomethingAbc(param1);
    }
    else if(type.Equals(enum2.def))
    {
        doSomethingDef(param1);
    }
}

private void doSomethingAbc(int param1) { ... }
private void doSomethingDef(int param1) { ... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM