内容简介:代码日志版权声明:翻译自: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
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
面向对象分析与设计
Grady Booch、Robert A. Maksimchuk、Michael W. Engel、Bobbi J. Young、Jim Conallen、Kelli A. Houston / 王海鹏、潘加宇 / 人民邮电出版社 / 2009-8 / 79.00元
《面向对象分析与设计(第3版)》是UML创始人Grady Booch的代表作之一,书中介绍的概念都基于牢固的理论基础。同时,《面向对象分析与设计(第3版)》又是一本注重实效的书,面向架构师和软件开发者等软件工程实践者的实际需要。《面向对象分析与设计(第3版)》通过大量例子说明了基本概念,解释了方法,并展示了在不同领域的成功应用。全书分为理论和应用两部分。理论部分深刻剖析了面向对象分析与设计(OOA......一起来看看 《面向对象分析与设计》 这本书的介绍吧!