使用模板方法代替重复代码

分享于2023年03月08日 c# generics reusability templates 问答
【问题标题】:Use a Template method in place of repeated code使用模板方法代替重复代码
【发布时间】:2023-03-07 22:03:01
【问题描述】:

我有 4 种代码相似的方法

private void LogExceptions(ObjA.Input input, int customerId)
{
    //ObjA is a big object, thats why I try not to send the whole object in this method
    Log(input);
    Log(ObjA.Exceptions);
}

private void LogExceptions(ObjB.Input input, int customerId)
{
    //ObjB is a big object, thats why I try not to send the whole object in this method
    Log(input);
    Log(ObjB.Exceptions);
}

等等

我无法使其成为模板方法,例如

private void LogExceptions(T1 input, int customerId) whereas     T1:ObjA.Input,ObjB.Input
{
    Log(T1);
    Log(T2);
}

怎么做或有其他方法吗? 提前感谢任何帮助。

我不认为我的问题有助于获得正确的答案.... 这是确切的代码....

    private void LogExceptions(AccARef.Response response)
    {
        StringBuilder sbErrors = null;

        if (response.ValMethod != null && response.ValMethod.IsValid == false)
        {
            if (response.ValMethod.Errors.Count() > 0)
            {
                sbErrors = new StringBuilder();
                foreach (AccARef.Exception exp in response.ValMethod.Errors)
                {
                    sbErrors.Append(" * " + exp.Message + exp.StackTrace + " ");
                    Console.WriteLine(strError.ToString())
                }
            }
        }
    }

    private void LogExceptions(AccBRef.Response response)
    {
        StringBuilder sbErrors = null;

        if (response.ValMethod != null && response.ValMethod.IsValid == false)
        {
            if (response.ValMethod.Errors.Count() > 0)
            {
                sbErrors = new StringBuilder();
                foreach (AccBRef.Exception exp in response.ValMethod.Errors)
                {
                    sbErrors.Append(" * " + exp.Message + exp.StackTrace + " ");
                    Console.WriteLine(strError.ToString())
                }
            }
        }
    }

现在 AcctBRef 和 A​​cctARef 不能实现通用接口,因为它们不是我的对象。或者如果它们不是我的东西,我还能把它们装饰成我的吗?

  • 您对“发送整个对象”的评论是什么意思?类实例是 passed by value-reference ,这意味着无论对象有多大,在方法之间传递的数据(指针)都非常少。
  • ObjB.Exceptions 是静态调用吗?
  • 另外,“Input”是 ObjA 和 ObjB 的内部类吗??
  • ObjA.Exceptions 只是一个示例,因为我无法输入对您没有意义的真实代码。
  • Input 是一个内部类,但它们都是独立的,来自不同的命名空间,但具有完全相同的属性。我无法更改它们或具有通用界面的原因,因为它们来自我无法控制的服务响应。

【解决方案1】:

我的感觉是,如果有 4 种方法并且它们没有相同的方法签名,它完全没问题,它并不总是必须是通用的,它也必须是可读的。

如果你只需要 Log(OneofTheTypeWhichYouKnowWhenCallingTheMethod) ,你为什么要打4个电话 Log(T1),Log(T2),Log(T3),Log(T4)

话虽如此,您总是可以像您的情况一样进行反思。

【讨论】: