c# – 如何重构这些具有一行差异的函数

栏目: C# · 发布时间: 5年前

内容简介:代码日志版权声明:翻译自:http://stackoverflow.com/questions/4880719/how-to-refactor-these-functions-which-have-one-line-difference

我有3个功能,其中唯一的区别是我指出的值与评论

//-- point of difference

所有这三个功能的大部分功能是一样的. “干”因素正在困扰我的睡眠:).我在想;这些可以容易地和可读地并入吗?

我以前有这样的情况,我希望在这里学习东西.

private string RenderRequestType(string render, NameValueCollection nvp, string prefix, string regexWild, string suffix)
{
    string regex = prefix + regexWild + suffix;

    MatchCollection matches = Regex.Matches(render, regex);

    foreach (Match match in matches)
    {
        foreach (Capture capture in match.Captures)
        {
            string name = capture.Value.Replace(prefix, "", StringComparison.CurrentCultureIgnoreCase).Replace(suffix, "", StringComparison.CurrentCultureIgnoreCase);

            //-- point of difference
            string value = nvp[name];

            render = render.Replace(capture.Value, value);
        }
    }

    return render;
}

private string RenderSessionType(string render, HttpContext httpContext, string prefix, string regexWild, string suffix)
{
    string regex = prefix + regexWild + suffix;

    MatchCollection matches = Regex.Matches(render, regex);

    foreach (Match match in matches)
    {
        foreach (Capture capture in match.Captures)
        {
            string name = capture.Value.Replace(prefix, "", StringComparison.CurrentCultureIgnoreCase).Replace(suffix, "", StringComparison.CurrentCultureIgnoreCase);

            //-- point of difference
            object session = httpContext.Session[name];
            string value = (session != null ? session.ToString() : "");

            render = render.Replace(capture.Value, value);
        }
    }

    return render;
}

private string RenderCookieType(string render, HttpContext httpContext, string prefix, string regexWild, string suffix)
{
    string regex = prefix + regexWild + suffix;

    MatchCollection matches = Regex.Matches(render, regex);

    foreach (Match match in matches)
    {
        foreach (Capture capture in match.Captures)
        {
            string name = capture.Value.Replace(prefix, "", StringComparison.CurrentCultureIgnoreCase).Replace(suffix, "", StringComparison.CurrentCultureIgnoreCase);

            //-- point of difference
            HttpCookie cookie = httpContext.Request.Cookies[name];
            string value = (cookie != null ? cookie.Value : "");

            render = render.Replace(capture.Value, value);
        }
    }

    return render;
}
您可以修改函数以获取Func<

string,string>做查找:

private string RenderType(string render, Func<string, string> lookupFunc, string prefix, string regexWild, string suffix)
{
    string regex = prefix + regexWild + suffix;

    MatchCollection matches = Regex.Matches(render, regex);

    foreach (Match match in matches)
    {
        foreach (Capture capture in match.Captures)
        {
            string name = capture.Value.Replace(prefix, "", StringComparison.CurrentCultureIgnoreCase).Replace(suffix, "", StringComparison.CurrentCultureIgnoreCase);

            //-- point of difference
            string value = lookupFunc(name);

            render = render.Replace(capture.Value, value);
        }
    }

    return render;
}

然后根据这个功能编写你的功能,例如:

private string RenderRequestType(string render, NameValueCollection nvp, string prefix, string regexWild, string suffix)
{
    return RenderType(render, name => nvp[name], prefix, regexWild, suffix);
}

代码日志版权声明:

翻译自:http://stackoverflow.com/questions/4880719/how-to-refactor-these-functions-which-have-one-line-difference


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

可爱的Python

可爱的Python

哲思社区 / 电子工业出版社 / 2009-9 / 55.00元

本书的内容主要来自CPyUG社区的邮件列表,由Python的行者根据自身经验组织而成,是为从来没有听说过Python的其他语言程序员准备的一份实用的导学性质的书。笔者试图将优化后的学习体验,通过故事的方式传达给读者,同时也分享了蟒样(Pythonic式)的知识获取技巧,而且希望将最常用的代码和思路,通过作弊条(Cheat Sheet,提示表单)的形式分享给有初步基础的Python 用户,来帮助大家......一起来看看 《可爱的Python》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

html转js在线工具
html转js在线工具

html转js在线工具