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

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

内容简介:代码日志版权声明:翻译自: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


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

查看所有标签

猜你喜欢:

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

面向对象葵花宝典:思想、技巧与实践

面向对象葵花宝典:思想、技巧与实践

李运华 编著 / 电子工业出版社 / 2015-12 / 69

《面向对象葵花宝典:思想、技巧与实践》系统地讲述了面向对象技术的相关内容,包括面向对象的基本概念、面向对象开发的流程、面向对象的各种技巧,以及如何应用面向对象思想进行架构设计。在讲述相关知识或技术的时候,除了从“是什么”这个角度进行介绍外,更加着重于从“为什么”和“如何用”这两个角度进行剖析,力争让读者做到“知其然,并知其所以然”,从而达到在实践中既能正确又能优秀地应用面向对象的相关技术和技巧。 ......一起来看看 《面向对象葵花宝典:思想、技巧与实践》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具