内容简介:代码日志版权声明:翻译自: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
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
数据结构、算法与应用(原书第2版)
Sartaj Sahni / 王立柱、刘志红 / 机械工业出版社 / 2015-4 / 79.00元
《数据结构、算法与应用——C++语言描述》是享有盛誉的数据结构教科书的第2版。它完整地包含了基本数据结构的内容,是CS2课程的理想用书。作者Sartaj Sahni通过循循善诱的讲解、直观具体的讨论和基于现实的应用,让读者轻松、愉快地学习。新版书着重利用标准模板库(STL),把书中开发的数据结构和算法与相应的STL实现方法相互关联。本书还增加了很多新的实例和练习题。 书中的应用实例是它的特色......一起来看看 《数据结构、算法与应用(原书第2版)》 这本书的介绍吧!