我正在開發一種解決方案,它將連接到各種服務器以讀取數據並執行操作。有許多變量使得可靠的通信複雜化,例如防火牆,停止/失敗的服務,認證差異以及各種軟件配置。我可以使用一些方法來解決這些問題,但在執行時不知道哪些方法會證明是成功的。多次實施嘗試
我的目標是創建一個可用於執行操作的接口和實現。第一個方法調用將用於大多數設備之後的最快實現,其次是可以處理前面列出問題的其他調用。
在一個完美的世界中,將編寫這個過程來快速確定哪個方法會成功,但是在我的測試中,只需要處理很多時間就像捕捉異常一樣。雖然性能始終是一個考慮因素,但最終,任務成功完成更爲重要。
下面是我創建的一個示例,它演示了遍歷實現列表的最壞情況場景。雖然這適用於一種方法,但在20個或更多不同操作中使用時,它不遵循DRY原則。一個可能的解決方案是Unity和攔截,但我發現調用處理程序中的invoke方法使用已解析的實現,而不是可能的實現列表。除非我錯過了一些東西,否則這似乎不是一種選擇。另外,我將需要遵循這個模式的幾個接口,所以創建一個可以遍歷實現列表的通用處理程序會很好。
有關如何完成此任務的任何建議,將不勝感激!
接口
public interface IProcess
{
int ProcessItem(string workType);
}
實現
public class ProcessImplementation1 : IProcess
{
public int ProcessItem(string workType)
{
throw new TimeoutException("Took too long");
}
}
public class ProcessImplementation2 : IProcess
{
public int ProcessItem(string workType)
{
throw new Exception("Unexpected issue");
}
}
public class ProcessImplementation3 : IProcess
{
public int ProcessItem(string workType)
{
return 123;
}
}
特別實施通過其他實現循環,直到一個成功,無一例外
public class ProcessImplementation : IProcess
{
public int ProcessItem(string workType)
{
List<IProcess> Implementations = new List<IProcess>();
Implementations.Add(new ProcessImplementation1());
Implementations.Add(new ProcessImplementation2());
Implementations.Add(new ProcessImplementation3());
int ProcessId = -1;
foreach (IProcess CurrentImplementation in Implementations)
{
Console.WriteLine("Attempt using {0} with workType '{1}'...",
CurrentImplementation.GetType().Name, workType);
try
{
ProcessId = CurrentImplementation.ProcessItem(workType);
break;
}
catch (Exception ex)
{
Console.WriteLine(" Failed: {0} - {1}.",
ex.GetType(), ex.Message);
}
Console.WriteLine();
if (ProcessId > -1)
{
Console.WriteLine(" Success: ProcessId {0}.", ProcessId);
}
else
{
Console.WriteLine("Failed!");
}
return ProcessId;
}
}
}
我要建議的複合模式,當仔細閱讀我意識到你會已經實施了它。 – TrueWill
@Maxim - 你爲什麼要刪除你的責任鏈模式答案?我打算贊成這個! – TrueWill