我有某些代碼模式(哪些日誌性能和每個函數的其他變量)需要在每個函數中出現,我不想一遍又一遍地重複代碼。下面是代碼的樣子:重複的代碼模式
public OutClass FUNC-X
{
if (IsDebugEnabled)
{
Logger("Start DataLibrary: FUNC-X");
}
try
{
CheckInitSucceeded();
GetAuthenticationTokens();
var dm = new Manager();
/**
* THIS SINGLE LINE IS THE VARIABLE PART
**/
var output = dm.FUNC-X(...);
if (IsDebugEnabled)
{
var data = Serialize(output);
Logger(output);
}
return output;
}
catch (WebFaultException)
{
throw;
}
catch (OtherException ex)
{
if (Logger.IsErrorEnabled)
{
Logger.LogError("Exception in FUNC-X", ex);
}
throw new OtherException("Some Message");
}
catch (Exception ex)
{
if (IsErrorEnabled)
{
Logger("Exception in FUNC-X", ex);
}
throw new Exception("Generic Exception");
}
finally
{
if (IsDebugEnabled)
{
Logger("End FUNC-X");
}
}
}
從本質上講,我只需要FUNC-Y或FUNC-Z到處這個名字出現,以取代FUNC-X,是有某種設計模式,我可以遵循?
如果問題很模糊,我很抱歉,我很樂意提供您提出的任何細節。
在我看來,你只是在某種工廠模式之後..但沒有一個更好的例子,很難說.. –
所以我想要的是,只要我創建一個新的功能,而不是複製和粘貼代碼我應該能夠告訴函數的名稱,它會自動完成用'Func-Y'替換'Func-X'的部分。 –
結果總是OutClass類嗎?或者那個類取決於所謂的函數? – fcuesta