我有某些代码模式(哪些日志性能和每个函数的其他变量)需要在每个函数中出现,我不想一遍又一遍地重复代码。下面是代码的样子:重复的代码模式
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