Hod*_*won 4 c# refactoring dictionary factory switch-statement
我正在尝试从文本文件中运行'Recipe'读取并逐行解析以动态调用一系列方法.我想在做了一些谷歌搜索后我需要实现一个工厂,但我缺少一些关键细节.这是我最接近的例子:
http://simpleprogrammer.com/2010/08/17/pulling-out-the-switch-its-time-for-a-whooping/
以下代码是现在的代码片段.
internal static void Run(int Thread_ID, List<StringBuilder> InstructionSet, List<double>[] Waveforms)
{
//Init
List<double>[] Register = new List<double>[10];
for (int i = 0; i < Waveforms.Length; i++) { Register[i] = new List<double>(Waveforms[i]); }
for (int i = 0; i < Register.Length; i++) { if (Register[i] == null) { Register[i] = new List<double>(); } }
//Run Recipe Steps
foreach (var item in InstructionSet)
{
Step Op = Step.Parse(item.ToString());
switch (Op.TaskName)
{
case "SimpleMovingAverage":
Register[Convert.ToInt32(Op.Args[0])] = Signal_Filters.SimpleMovingAverage(Register[Convert.ToInt32(Op.Args[1])], Convert.ToInt32(Op.Args[2]));
break;
case "RollingSteppedStdDeviation":
Register[Convert.ToInt32(Op.Args[0])] = Signal_Filters.RollingSteppedStdDeviation(Register[Convert.ToInt32(Op.Args[1])], Convert.ToInt32(Op.Args[2]), Convert.ToInt32(Op.Args[3]));
break;
//... etc. many, many methods to be called.
}
}
}
Run Code Online (Sandbox Code Playgroud)
......以下是我对以下问题的示例部分:
public static class MoveFactory
{
private static Dictionary<string, Func<IMove>> moveMap = new Dictionary<string, Func<IMove>>()
{
{"Up", () => { return new UpMove(); }},
{"Down", () => { return new DownMove(); }},
{"Left", () => { return new LeftMove(); }}
// ...
};
public static IMove CreateMoveFromName(string name)
{
return moveMap[name]();
}
}
Run Code Online (Sandbox Code Playgroud)
我可以自动生成字典列表吗?因此,每当我添加一个实现我的工厂接口的新类(我相当于IMove)时,我就不必更新我的字典或我的代码的任何其他部分.也许这可以作为界面的一部分被迫?
在上面的示例代码中,我没有看到它传入和传出参数.看看我的代码我有数据我需要逐步变异...我怎么用工厂做这个.
工厂需要是线程安全的,因为我想将不同的初始数据传递给多个工作人员,每个工作人员都运行自己的配方.
让我们一次解决这个问题.
创建属性非常简单,所以我会留给你查找,但我们假设你有一个MoveNameAttribute可以在类级别应用的调用.然后,您可以装饰IMove像这样实现的类:
[MoveName("Up")]
class UpMove: IMove{}
[MoveName("Down")]
class DownMove: IMove{}
Run Code Online (Sandbox Code Playgroud)
现在,您可以使用Reflection和一些LINQ将这些类类型提取到字典中,并使用自定义属性中指定的键按需创建这些类型的新实例.
虽然整个工厂本身在代码行方面很短,但如果你以前从未做过反思,那么反思可能是令人生畏的.我已经注释了每一行来解释发生了什么.
internal static class MoveFactory
{
private static readonly IDictionary<String, Type> _moveTypes;
static MoveFactory()
{
_moveTypes = LoadAllMoveTypes();
}
private static IDictionary<string, Type> LoadAllMoveTypes()
{
var asm =
//Get all types in the current assembly
from type in Assembly.GetExecutingAssembly().GetTypes()
//Where the type is a class and implements "IMove"
where type.IsClass && type.GetInterface("IMove") != null
//Only select types that are decorated with our custom attribute
let attr = type.GetCustomAttribute<MoveNameAttribute>()
where attr != null
//Return both the Name and the System.Type
select new
{
name = attr.Name,
type
};
//Convert the results to a Dictionary with the Name as a key
// and the Type as the value
return asm.ToDictionary(move => move.name, move => move.type);
}
internal static IMove CreateMove(String name)
{
Type moveType;
//Check to see if we have an IMove with the specific key
if(_moveTypes.TryGetValue(name, out moveType))
{
//Use reflection to create a new instance of that IMove
return (IMove) Activator.CreateInstance(moveType);
}
throw new ArgumentException(
String.Format("Unable to locate move named: {0}", name));
}
}
Run Code Online (Sandbox Code Playgroud)
现在你有了工厂,你可以简单地创建这样的新实例:
var upMove = MoveFactory.CreateMove("Up");
var downMove = MoveFactory.CreateMove("Down");
Run Code Online (Sandbox Code Playgroud)
由于工厂使用静态构造函数,它只会填充此列表一次,并会自动获取新类.
我不是100%确定你的用例是什么,但看起来你不需要将参数传递给你的工厂,而不是你的工具上的某些方法IMove.但是,您可以传入可变数量的参数.
如果是这种情况,那么你只需要在设计中忍受一点丑陋.您的IMove界面需要一个非常通用的方法:
public interface IMove
{
double Compute(double val1, params int[] args);
}
Run Code Online (Sandbox Code Playgroud)
现在,您的个人移动课程必须勤奋并检查以确保他们获得适当数量的参数.我会把这作为练习留给你,但这应该根据上面的例子给你你需要的东西.
实际上,上面的工厂实现是线程安全的,因为它不依赖于任何共享状态,并且底层字典本质上是不可变的.每次调用都会CreateMove返回一个全新的IMove实例.
现在你的实现是否IMove是线程安全的取决于你:)
呼!这是一个很长的答案,但希望这会帮助你.