[英]What's the better function triage method: Switch-case, or Dictionary?
[EDIT: The code examples below are language-agnostic, although specific answers regarding java
and C#
are encouraged.] [编辑:下面的代码示例与语言无关,尽管鼓励有关
java
和C#
特定答案。
In my game, there's a situation where the code uses a dynamic parameter value (from somewhere else) to decide which function to call from a number of candidates. 在我的游戏中,有一种情况是代码使用动态参数值(从其他位置开始)来决定要从多个候选对象中调用哪个函数。 Something like:
就像是:
string theParam = parameterFromSomewhereElse;
if(theParam == "apple")
callingFunctionFor_apple();
else if(theParam == "banana")
callingFunctionFor_banana();
else if(theParam == "cat")
callingFunctionFor_cat();
Obviously, one way to do that is the above mentioned if-else
, or even cleaner, switch-case
, but I want to know if there are even better ways, if theParam
can have, like, fifty values. 显然,一种实现方法是上述的
if-else
,甚至更干净的switch-case
,但是我想知道,如果theParam
可以有五十个值,是否还有更好的方法。
Two of my thoughts were: 我的两个想法是:
1) Something like a "dynamic named function calling"? 1)类似于“动态命名函数调用”?
string theParam = parameterFromSomewhereElse;
callFunction("callingFunctionFor_" + theParam);
/// where callFunction is some language defined utility?
2) Make a dictionary, with values as functions 2)制作一个字典,以值作为函数
global_dict = {
"apple": callingFunctionFor_apple(),
"banana": callingFunctionFor_banana(),
"cat": callingFunctionFor_cat()
}
and then, simply, 然后,简单地,
global_dict[theParam](); // or something similar, ignore the syntax
Looked at these questions: 看了这些问题:
Comments in both indicate a preference to a normal switch-case
, something like: 两者中的注释均表示优先使用普通的
switch-case
,例如:
string theParam = parameterFromSomewhereElse;
switch theParam:
case "apple":
callingFunctionFor_apple();
break;
case "banana":
callingFunctionFor_banana();
break;
case "cat":
callingFunctionFor_cat();
break;
But, in my opinion, it's only visually a little cleaner than if-else
(if not the same, because of the break
s littering throughout.) I'd thought a global dictionary, one place to store all the function references, would be a preferred solution, but I sense there's something more to the tale. 但是,在我看来,这只是视觉上造成不小的吸尘器
if-else
(如果不相同,因为的break
整个小号乱扔垃圾。)我还以为一个全球性的词典,一个用于存储所有的函数引用的地方,将是一个首选的解决方案,但我觉得这个故事还有更多的东西。
What do you recommend? 您有什么推荐的吗?
Below code can give some helping hand for you its written purely in c# 下面的代码可以为您提供帮助,它完全是用C#编写的
public class InstanceCreator
{
/// <summary>
/// Get the Instance from a fully qualified Name
/// </summary>
/// <param name="strFullyQualifiedName"></param>
/// <returns></returns>
public object GetInstance(string strFullyQualifiedName)
{
Type t = Type.GetType(strFullyQualifiedName);
return Activator.CreateInstance(t);
}
/// <summary>
/// Invoking the Method By Passing the Instance as well as the Method Name
/// </summary>
/// <param name="Instance">Instance Object</param>
/// <param name="MethodName">Method Name</param>
/// <returns></returns>
public object InvokeMethod(object Instance, String MethodName) {
Type t = Instance.GetType();
MethodInfo method = t.GetMethod(MethodName);
return method.Invoke(Instance, null);
}
}
public class SampleClass {
public bool doAction() {
return true;
}
}
public class TestClass {
public Dictionary<String, String> FunctionList = new Dictionary<string, string>();
public TestClass() {
this.Verify();
}
public void Verify() {
String fullyQualifiedName = typeof(SampleClass).AssemblyQualifiedName;
FunctionList.Add("SAMPLE", $"{fullyQualifiedName}#doAction");
InstanceCreator Creator = new InstanceCreator();
String[] FunctionMapping = FunctionList["SAMPLE"].Split('#');
object Test = Creator.GetInstance(FunctionMapping[0]);
bool Result = (bool)Creator.InvokeMethod(Test, FunctionMapping[1]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.