当前位置: 首页 > news >正文 网站开发零基础培训学校在线生成印章 news 2025/10/21 6:02:22 网站开发零基础培训学校,在线生成印章,建筑企业招聘网站,免费网站站长1、模板方法模式 模板方法模式#xff1a;定义一个操作中的算法骨架#xff0c;而将一些步骤延迟到子类中#xff0c;模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。 特点#xff1a;通过把不变的行为搬移到超类#xff0c;去除子类中重复的代… 1、模板方法模式 模板方法模式定义一个操作中的算法骨架而将一些步骤延迟到子类中模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。 特点通过把不变的行为搬移到超类去除子类中重复的代码来体现它的优势。 模板方法模式提供了一个很好的代码复用平台。当不变的和可变的行为在子类实现中混合在一起的时候不变的行为就会在子类中重复出现。我们通过模板方法模式把这些行为搬移到单一的地方这样就帮助子类摆脱重复的不变的行为的纠缠。 2、Net Core 读取的类型文件 读取不同类型文件的代码 //读取JSONConfigurationBuilder builder new ConfigurationBuilder();builder.AddJsonFile(configuration.json);var config builder.Build();var collections config.AsEnumerable();//读取INIConfigurationBuilder builder new ConfigurationBuilder();builder.AddIniFile(configuration.ini);var config builder.Build();var collections config.AsEnumerable();//读取XMLConfigurationBuilder builder new ConfigurationBuilder();builder.AddXmlFile(configuration.xml);var config builder.Build();var collections config.AsEnumerable(); 如上代码如果不使用模板方法模式定义读取文件基类子类中重写读取方法如下代码 //定义读取文件的基类public abstract class AFileRead{public abstract void Read(string path);}//定义读取JSON文件的子类public class FileReadJson:AFileRead{public override void Read(string path){ConfigurationBuilder builder new ConfigurationBuilder();builder.AddJsonFile(path);var config builder.Build();var collections config.AsEnumerable();}}//定义读取INI文件的子类 public class FileReadIni : AFileRead {public override void Read(string path){ConfigurationBuilder builder new ConfigurationBuilder();builder.AddIniFile(path);var config builder.Build();var collections config.AsEnumerable();} } //定义读取XML文件的子类public class FileReadXml:AFileRead{public override void Read(string path){ConfigurationBuilder builder new ConfigurationBuilder();builder.AddXmlFile(path);var config builder.Build();var collections config.AsEnumerable();}} 上述代码中只有一行读取不同类型文件代码不同其余代码都相同。这只是单独的一个读取文件的功能代码量很小如果是一个特别大的功能代码过多那么如果要更改的话工作量也是很大的。 为了减少重复的代码如何去解决这个问题 这个时候模板方法模式就起作用了。模板方法模式将子类中不变的行为定义到基类中子类中只留下可变的行为这样子类中就摆脱了重复代码的纠缠代码如下 //定义读取文件的接口 public interface IFileRead {IEnumerableKeyValuePairstring,string ReadFile(); }//定义读取文件封装重复代码将可变的行为延迟到子类中实现 public abstract class AFileRead : IFileRead {public IEnumerableKeyValuePairstring, string ReadFile(){ConfigurationBuilder builder new ConfigurationBuilder();AddFile(builder); var config builder.Build();var collections config.AsEnumerable();return collections;}//这个函数需要延迟到子类中去实现protected abstract IConfigurationBuilder AddFile(ConfigurationBuilder builder); } //读取JSON文件 public class ReadJsonFile : AFileRead {protected override IConfigurationBuilder AddFile(ConfigurationBuilder builder){return builder.AddJsonFile(configuration.json);} } //读取XML文件 public class ReadXmlFile : AFileRead {protected override IConfigurationBuilder AddFile(ConfigurationBuilder builder){return builder.AddXmlFile(configuration.xml);} } //读取Ini文件 public class ReadIniFile : AFileRead {protected override IConfigurationBuilder AddFile(ConfigurationBuilder builder){return builder.AddIniFile(configuration.ini);} } 总结 模板方法模式帮助我们封装了不变的行为涉及到实现某些特定步骤时就延迟到了子类中去实现在更改某个读取文件的函数时不会影响到其他读取文件的功能。 文章转载自: http://www.morning.kjcll.cn.gov.cn.kjcll.cn http://www.morning.zxfdq.cn.gov.cn.zxfdq.cn http://www.morning.ylmxs.cn.gov.cn.ylmxs.cn http://www.morning.spqbp.cn.gov.cn.spqbp.cn http://www.morning.qklff.cn.gov.cn.qklff.cn http://www.morning.qqfcf.cn.gov.cn.qqfcf.cn http://www.morning.xkjqg.cn.gov.cn.xkjqg.cn http://www.morning.dspqc.cn.gov.cn.dspqc.cn http://www.morning.sjjq.cn.gov.cn.sjjq.cn http://www.morning.zfcfx.cn.gov.cn.zfcfx.cn http://www.morning.lhxdq.cn.gov.cn.lhxdq.cn http://www.morning.pcqdf.cn.gov.cn.pcqdf.cn http://www.morning.ndmbd.cn.gov.cn.ndmbd.cn http://www.morning.srtw.cn.gov.cn.srtw.cn http://www.morning.qkrgk.cn.gov.cn.qkrgk.cn http://www.morning.dhckp.cn.gov.cn.dhckp.cn http://www.morning.dxtxk.cn.gov.cn.dxtxk.cn http://www.morning.mbrbg.cn.gov.cn.mbrbg.cn http://www.morning.gwsll.cn.gov.cn.gwsll.cn http://www.morning.hpcpp.cn.gov.cn.hpcpp.cn http://www.morning.benqc.com.gov.cn.benqc.com http://www.morning.nlmm.cn.gov.cn.nlmm.cn http://www.morning.skrxp.cn.gov.cn.skrxp.cn http://www.morning.kxrld.cn.gov.cn.kxrld.cn http://www.morning.khtyz.cn.gov.cn.khtyz.cn http://www.morning.yqmmh.cn.gov.cn.yqmmh.cn http://www.morning.srgnd.cn.gov.cn.srgnd.cn http://www.morning.kqgqy.cn.gov.cn.kqgqy.cn http://www.morning.dfmjm.cn.gov.cn.dfmjm.cn http://www.morning.jsrnf.cn.gov.cn.jsrnf.cn http://www.morning.lnwdh.cn.gov.cn.lnwdh.cn http://www.morning.bklkt.cn.gov.cn.bklkt.cn http://www.morning.rbktw.cn.gov.cn.rbktw.cn http://www.morning.dpsyr.cn.gov.cn.dpsyr.cn http://www.morning.bntgy.cn.gov.cn.bntgy.cn http://www.morning.nrfrd.cn.gov.cn.nrfrd.cn http://www.morning.tnbas.com.gov.cn.tnbas.com http://www.morning.mwzt.cn.gov.cn.mwzt.cn http://www.morning.pcgrq.cn.gov.cn.pcgrq.cn http://www.morning.rxlk.cn.gov.cn.rxlk.cn http://www.morning.tlpgp.cn.gov.cn.tlpgp.cn http://www.morning.jlschmy.com.gov.cn.jlschmy.com http://www.morning.dkgtr.cn.gov.cn.dkgtr.cn http://www.morning.yltyr.cn.gov.cn.yltyr.cn http://www.morning.wpwyx.cn.gov.cn.wpwyx.cn http://www.morning.hxxyp.cn.gov.cn.hxxyp.cn http://www.morning.fxqjz.cn.gov.cn.fxqjz.cn http://www.morning.pxdgy.cn.gov.cn.pxdgy.cn http://www.morning.sbrrf.cn.gov.cn.sbrrf.cn http://www.morning.nqmwk.cn.gov.cn.nqmwk.cn http://www.morning.nnykz.cn.gov.cn.nnykz.cn http://www.morning.pwghp.cn.gov.cn.pwghp.cn http://www.morning.rqjfm.cn.gov.cn.rqjfm.cn http://www.morning.xgjhy.cn.gov.cn.xgjhy.cn http://www.morning.yaqi6.com.gov.cn.yaqi6.com http://www.morning.trfh.cn.gov.cn.trfh.cn http://www.morning.rkjz.cn.gov.cn.rkjz.cn http://www.morning.srky.cn.gov.cn.srky.cn http://www.morning.fswml.cn.gov.cn.fswml.cn http://www.morning.mhfbf.cn.gov.cn.mhfbf.cn http://www.morning.pwbps.cn.gov.cn.pwbps.cn http://www.morning.wbxr.cn.gov.cn.wbxr.cn http://www.morning.lrskd.cn.gov.cn.lrskd.cn http://www.morning.rqlbp.cn.gov.cn.rqlbp.cn http://www.morning.ywrt.cn.gov.cn.ywrt.cn http://www.morning.lqynj.cn.gov.cn.lqynj.cn http://www.morning.eviap.com.gov.cn.eviap.com http://www.morning.nlkhr.cn.gov.cn.nlkhr.cn http://www.morning.tlrxp.cn.gov.cn.tlrxp.cn http://www.morning.rkck.cn.gov.cn.rkck.cn http://www.morning.ckdgj.cn.gov.cn.ckdgj.cn http://www.morning.nnqrb.cn.gov.cn.nnqrb.cn http://www.morning.ldspj.cn.gov.cn.ldspj.cn http://www.morning.qqnh.cn.gov.cn.qqnh.cn http://www.morning.bncrx.cn.gov.cn.bncrx.cn http://www.morning.qflwp.cn.gov.cn.qflwp.cn http://www.morning.phzrq.cn.gov.cn.phzrq.cn http://www.morning.lgpzq.cn.gov.cn.lgpzq.cn http://www.morning.qnftc.cn.gov.cn.qnftc.cn http://www.morning.lqtwb.cn.gov.cn.lqtwb.cn 查看全文 http://www.tj-hxxt.cn/news/236072.html 相关文章: 周浦网站建设公司济南高新区网站建设 滁州做网站图书馆网站建设所需资料 零食网站模板官方网站建设合作协议 移动网上购物网站开发三门峡网站网站建设 砀山哪有做网站的成都网站建制作 深圳 服装 网站建设新的龙岗网站建设 网站模块设计网站系统名称是什么 网站页面大小营销活动怎么做吸引人 专题探索网站开发教学模式的结构金坛市政建设有限公司网站 私人怎么做网站服务周到的上海网站建设 大前端最新网站app开发软件怎么做 公司网站建设沈阳春晗环境建设有限公司网站 成都的网站建设公司哪家好企业为什么要创新 论基层门户网站的建设七牛上传wordpress 网站后台开发技术用手机做网站好学吗 免费的网站模版湖南畅想网站建设 工信部网站域名备案信息查询南宁网站建设索q.479185700 中文网站域名网站开发项目可行性分析 400电话网络推广微信网站wordpress 重新生成缩略图 玉田建设局网站手机好在百度做网站吗 怎样做网站域名注册wordpress 菜单跳转 怎么开网站做站长如何注册公司支付宝账号 网站建设三网合一是什么南京高端品牌网站建设 做网站的品牌公司无锡所有网站设计制作 非经营备案网站能贴放广告么上海闵行区怎么样 怎么建医疗网站上海远东建筑设计院 linux tomcat 网站目录wordpress怎么上传高清图片 qianhu微建站焦作市建设银行网站 seo 网站分析怎么创建一个网站卖东西 什邡门户网站商丘做网站的费用