如果" .NET 4.0优化代码重构现有的"条件和与QUOT;是"操作者 [英] .Net 4.0 Optimized code for refactoring existing "if" conditions and "is" operator
问题描述
我有以下的C#代码。它工作正常;但 GetDestination()
方法是堆满了多个如果
条件使用的是运营商。
在.NET 4.0(或更高版本)是什么,以避免这些如果条件呢?
编辑:角色是商业模型的一部分,而目的地是纯粹使用商业模型中的一个特定的应用程序的一个工件
码
公共类角色{}
公共类经理:角色{}
公共类会计师:角色{}
公共类Attender:角色{}
公共类清洗剂:角色{}
公共类安全:角色{}
类节目
{
静态字符串GetDestination(角色X)
{
串目的地= @\Home
如果(x是经理)
{
目的地= @\ManagerHomeA
}
如果(x是会计师)
{
目的地= @\AccountantHomeC
}
如果(x是清洁)
{
目的地= @\Cleaner
}
返回目的地;
}
静态无效的主要(字串[] args)
{
串目的地= GetDestination(新会计师());
Console.WriteLine(目标);
到Console.ReadLine();
}
}
参考
方法1(选择):是动态
关键字落实多方法
/ 双重分派
方法二:使用字典
来避免如果
如下乔恩斯基特的答复中提到块
方法3:使用 HashList
与代表
如果有比其他相等(条件例如,如果输入< 25)。请参阅的如何重构一套<的=,> = if ... else语句转换成字典或类似的
Apporach东西4:在下面MarcinJuraszek的答复中提到虚拟功能
多方法/双调度使用动态关键字的方法
的依据的:在这里,基于类型的算法变化。也就是说,如果输入的是会计师,该功能将被执行比对经理的不同。
公共静态类DestinationHelper
{
公共静态字符串GetDestinationSepcificImplm(经理X)
{
返回@\ManagerHome
}
公共静态字符串GetDestinationSepcificImplm(会计师X)
{
返回@\AccountantHome
}
公共静态字符串GetDestinationSepcificImplm(清洁X)
{
返回@\CleanerHome
}
}
类节目
{
静态字符串GetDestination(角色X)
{
#地区其他一般作品
//做记录
//其他业务活动
#endregion
串目的地=的String.Empty;
动态inputRole = X;
=目的地DestinationHelper.GetDestinationSepcificImplm(inputRole);
返回目的地;
}
静态无效的主要(字串[] args)
{
串目的地= GetDestination(新安全());
Console.WriteLine(目标);
Console.WriteLine(......);
到Console.ReadLine();
}
}
I have following C# code. It works fine; but the GetDestination()
method is cluttered with multiple if
conditions by using is operator.
In .Net 4.0 (or greater) what is the best way to avoid these "if" conditions?
EDIT: Role is part of the business model, and the destination is purely an artifact of one particular application using that business model.
CODE
public class Role { }
public class Manager : Role { }
public class Accountant : Role { }
public class Attender : Role { }
public class Cleaner : Role { }
public class Security : Role { }
class Program
{
static string GetDestination(Role x)
{
string destination = @"\Home";
if (x is Manager)
{
destination = @"\ManagerHomeA";
}
if (x is Accountant)
{
destination = @"\AccountantHomeC";
}
if (x is Cleaner)
{
destination = @"\Cleaner";
}
return destination;
}
static void Main(string[] args)
{
string destination = GetDestination(new Accountant());
Console.WriteLine(destination);
Console.ReadLine();
}
}
REFERENCES
- Dictionary<T,Delegate> with Delegates of different types: Cleaner, non string method names?
- Jon Skeet: Making reflection fly and exploring delegates
- if-else vs. switch vs. Dictionary of delegates
- Dictionary with delegate or switch?
- Expression and delegate in c#
Approach 1 (Selected): Using dynamic
keyword to implement multimethods
/ double dispatch
Approach 2: Use a dictionary
to avoid if
blocks as mentioned in Jon Skeet’s answer below.
Approach 3: Use a HashList
with delegates
if there is condition other than equality (For example, if input < 25). Refer how to refactor a set of <= , >= if...else statements into a dictionary or something like that
Apporach 4: Virtual Functions as mentioned in MarcinJuraszek’s answer below.
MultiMethods / Double Dispatch approach using dynamic keyword
Rationale: Here the algorithm changes based on the type. That is, if the input is Accountant, the function to be executed is different than for Manager.
public static class DestinationHelper
{
public static string GetDestinationSepcificImplm(Manager x)
{
return @"\ManagerHome";
}
public static string GetDestinationSepcificImplm(Accountant x)
{
return @"\AccountantHome";
}
public static string GetDestinationSepcificImplm(Cleaner x)
{
return @"\CleanerHome";
}
}
class Program
{
static string GetDestination(Role x)
{
#region Other Common Works
//Do logging
//Other Business Activities
#endregion
string destination = String.Empty;
dynamic inputRole = x;
destination = DestinationHelper.GetDestinationSepcificImplm(inputRole);
return destination;
}
static void Main(string[] args)
{
string destination = GetDestination(new Security());
Console.WriteLine(destination);
Console.WriteLine("....");
Console.ReadLine();
}
}
这篇关于如果" .NET 4.0优化代码重构现有的"条件和与QUOT;是"操作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!