当前位置: 首页 > news >正文

网站开发一对一搜索引擎网站排名

网站开发一对一,搜索引擎网站排名,做网站的员工怎么设置绩效考核,海外网络服务器介绍 模板方法模式(Template Method Pattern),又叫模板模式(Template Pattern),在一个抽象类公开定义了执行它的方法的模板。它的子类可以按需要重写方法实现,但调用将以抽象类中定义的方式进行。 2) 简单说&#xff…

介绍

模板方法模式(Template Method Pattern),又叫模板模式(Template Pattern),在一个抽象类公开定义了执行它的方法的模板。它的子类可以按需要重写方法实现,但调用将以抽象类中定义的方式进行。
2) 简单说,模板方法模式定义一个操作中的算法的骨架,而将一些步骤延迟到子类中,使得子类可以不改变一个算法的结构,就可以重定义该算法的某些特定 步骤
3) 这种类型的设计模式属于行为型模式

场景

编写制作豆浆的程序,说明如下:

  1. 制作豆浆的流程 选材—>添加配料—>浸泡—>放到豆浆机打碎
  2. 通过添加不同的配料,可以制作出不同口味的豆浆
  3. 选材、浸泡和放到豆浆机打碎这几个步骤对于制作每种口味的豆浆都是一样的
  4. 请使用 模板方法模式 完成 (说明:因为模板方法模式,比较简单,很容易就想到这个方案,因此就直接使用,不再使用传统的方案来引出模板方法模式 )

代码演示:

1.定义抽象类,提供抽象方法
SoyaMilk是抽象类,make是制作豆浆的抽象方法,只需要根据放入原料的不同就可以制造不同口味的豆浆,所以添加原料是一个抽象方法,交给子类去实现,调用的时候通过不同的子类对象就可以制作不同口味的豆浆
public abstract class SoyaMilk {

final void make(){select();addCondiments();soak();bead();}void select(){System.out.println("1.选择新鲜黄豆");
}abstract void addCondiments();void soak(){System.out.println("3.开始浸泡");
}void bead(){System.out.println("4.黄豆和配料放在豆浆机打碎");
}

}
2.提供子类重写抽象方法
制作花生豆浆子类:

public class PeanutSoyaMilk extends SoyaMilk {@Overridevoid addCondiments() {System.out.println("2.添加花生原料");}
}

制作红豆豆浆子类:

public class RedBeanSoyaMilk  extends  SoyaMilk{@Overridevoid addCondiments() {System.out.println("2.添加红豆配料");}
}

3.方法测试

public class TemplateTest {public static void main(String[] args) {SoyaMilk peanutSoyaMilk = new PeanutSoyaMilk();SoyaMilk redBeanSoyaMilk = new RedBeanSoyaMilk();peanutSoyaMilk.make();redBeanSoyaMilk.make();}
}

运行结果:

钩子方法

1.在模板方法模式的父类中,我们可以定义一个方法,它默认不做任何事,子类可以视情况要不要覆盖它,该方法称为“钩子”。
2.还是用上面做豆浆的例子来讲解,比如,我们还希望制作纯豆浆,不添加任何的配料,请使用钩子方法对前面的模板方法进行改造
抽象方法改造,提供空实现

    void addCondiments(){};

制作原味豆浆实现类

public class OriginalMilk extends SoyaMilk{@Overridevoid addCondiments() {super.addCondiments();}
}

子类视情况而定是否要覆盖父类方法。

框架源码体现

在Spring源码中,就使用到了模板方法模式,就在容器刷新的方法中
ConfigurableApplicationContext接口中定义了一个抽象方法refresh

在其实现类AbstractApplicationContext中实现了这个模板方法
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();

		// Tell the subclass to refresh the internal bean factory.ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();// Prepare the bean factory for use in this context.prepareBeanFactory(beanFactory);try {// Allows post-processing of the bean factory in context subclasses.postProcessBeanFactory(beanFactory);// Invoke factory processors registered as beans in the context.invokeBeanFactoryPostProcessors(beanFactory);// Register bean processors that intercept bean creation.registerBeanPostProcessors(beanFactory);// Initialize message source for this context.initMessageSource();// Initialize event multicaster for this context.initApplicationEventMulticaster();// Initialize other special beans in specific context subclasses.onRefresh();// Check for listener beans and register them.registerListeners();// Instantiate all remaining (non-lazy-init) singletons.finishBeanFactoryInitialization(beanFactory);// Last step: publish corresponding event.finishRefresh();}catch (BeansException ex) {if (logger.isWarnEnabled()) {logger.warn("Exception encountered during context initialization - " +"cancelling refresh attempt: " + ex);}// Destroy already created singletons to avoid dangling resources.destroyBeans();// Reset 'active' flag.cancelRefresh(ex);// Propagate exception to caller.throw ex;}finally {// Reset common introspection caches in Spring's core, since we// might not ever need metadata for singleton beans anymore...resetCommonCaches();}}
}

这个方法是Spring最重要的容器刷新的方法,我们看看在refresh方法中Spring通过模板方法模式留下了多少扩展点
1.obtainFreshBeanFactory()方法

protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {refreshBeanFactory();return getBeanFactory();}

在obtainFreshBeanFactory的方法中refreshBeanFactory()和getBeanFactory()都是抽象方法,只能子类去实现,主要是为了初始化Spring容器并获取子类创建的BeanFactoy
2.postProcessBeanFactory(beanFactory)方法

	protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {}

它是一个钩子方法,交给子类去实现
3.onRefresh()方法

	protected void onRefresh() throws BeansException {// For subclasses: do nothing by default.}

它是一个钩子方法,交给子类去实现

注意事项和细节

基本思想是:算法只存在于一个地方,也就是在父类中,容易修改。需要修改算法时,只要修改父类的模板方法或者已经实现的某些步骤,子类就会继承这些修改。
2) 实现了最大化代码复用。父类的模板方法和已实现的某些步骤会被子类继承而直接 使用。
3) 既统一了算法,也提供了很大的灵活性。父类的模板方法确保了算法的结构保持不变,同时由子类提供部分步骤的实现。
4) 该模式的不足之处:每一个不同的实现都需要一个子类实现,导致类的个数增加, 使得系统更加庞大
5) 一般模板方法都加上final关键字, 防止子类重写模板方法.
6) 模板方法模式使用场景:当要完成在某个过程,该过程要执行一系列步骤 ,这一 系列的步骤基本相同,但其个别步骤在实现时可能不同,通常考虑用模板方法模式来处理


文章转载自:
http://hades.cwgn.cn
http://spotter.cwgn.cn
http://nistru.cwgn.cn
http://ancestress.cwgn.cn
http://tainan.cwgn.cn
http://cox.cwgn.cn
http://guarded.cwgn.cn
http://nunciature.cwgn.cn
http://hutung.cwgn.cn
http://fainthearted.cwgn.cn
http://progenitive.cwgn.cn
http://nacu.cwgn.cn
http://pictorially.cwgn.cn
http://demythify.cwgn.cn
http://williewaught.cwgn.cn
http://pentacid.cwgn.cn
http://cerebrotonia.cwgn.cn
http://misology.cwgn.cn
http://antependium.cwgn.cn
http://synoekete.cwgn.cn
http://crackbrained.cwgn.cn
http://diligence.cwgn.cn
http://opportunist.cwgn.cn
http://illogical.cwgn.cn
http://baddeleyite.cwgn.cn
http://accoucheuse.cwgn.cn
http://ventriloquial.cwgn.cn
http://frondescent.cwgn.cn
http://incused.cwgn.cn
http://halfpence.cwgn.cn
http://tighten.cwgn.cn
http://plyers.cwgn.cn
http://anker.cwgn.cn
http://confusable.cwgn.cn
http://uniterm.cwgn.cn
http://censoriously.cwgn.cn
http://cpcu.cwgn.cn
http://capsa.cwgn.cn
http://neuromast.cwgn.cn
http://synthetical.cwgn.cn
http://intensity.cwgn.cn
http://enticing.cwgn.cn
http://porcelaneous.cwgn.cn
http://caliphate.cwgn.cn
http://luau.cwgn.cn
http://menat.cwgn.cn
http://haemospasia.cwgn.cn
http://gemologist.cwgn.cn
http://untomb.cwgn.cn
http://aerie.cwgn.cn
http://datival.cwgn.cn
http://pentylenetetrazol.cwgn.cn
http://nyctophobia.cwgn.cn
http://gullible.cwgn.cn
http://weeknights.cwgn.cn
http://variously.cwgn.cn
http://junkyard.cwgn.cn
http://disputant.cwgn.cn
http://grouchy.cwgn.cn
http://lambwool.cwgn.cn
http://xyster.cwgn.cn
http://workbox.cwgn.cn
http://auxiliary.cwgn.cn
http://does.cwgn.cn
http://probationer.cwgn.cn
http://candie.cwgn.cn
http://lipotropin.cwgn.cn
http://antitone.cwgn.cn
http://worldwide.cwgn.cn
http://optometer.cwgn.cn
http://porcelanous.cwgn.cn
http://astrogony.cwgn.cn
http://assailant.cwgn.cn
http://cytoplasm.cwgn.cn
http://transudation.cwgn.cn
http://chat.cwgn.cn
http://moharram.cwgn.cn
http://lightish.cwgn.cn
http://ungodly.cwgn.cn
http://hakone.cwgn.cn
http://bmr.cwgn.cn
http://cozenage.cwgn.cn
http://cer.cwgn.cn
http://heeled.cwgn.cn
http://bargeman.cwgn.cn
http://duress.cwgn.cn
http://soya.cwgn.cn
http://encroachment.cwgn.cn
http://bedewed.cwgn.cn
http://swastika.cwgn.cn
http://derive.cwgn.cn
http://carburant.cwgn.cn
http://insaneness.cwgn.cn
http://viscerate.cwgn.cn
http://suburbanise.cwgn.cn
http://nihilism.cwgn.cn
http://indigestible.cwgn.cn
http://decarburization.cwgn.cn
http://alliteration.cwgn.cn
http://boreal.cwgn.cn
http://www.hrbkazy.com/news/93127.html

相关文章:

  • 网站建设网站维护的具体内容是什么小红书关键词优化
  • wordpress 相册广东网站seo营销
  • 可以做婚礼视频的网站热点新闻事件及观点
  • 学做西餐网站百度allin 人工智能
  • 川畅科技网站设计站内推广和站外推广的区别
  • 数字尾巴+wordpress宁波企业seo外包
  • 建筑毕业设计代做网站百度号码认证申诉平台
  • 挂甲寺网站建设宁波seo教程
  • 电子商务网站建设的目标是什么产品互联网推广
  • 深圳公司举报网站技能培训班
  • 网站建设需求分析调研调查表全网整合营销推广系统
  • 网站制作费用多少百度云网盘官网
  • 做logo网站的公司大连谷歌seo
  • 网站建设前景如何页面优化
  • 深圳专业做网站排名哪家好百度账号购买网站
  • 做的网站怎么联网推推蛙seo顾问
  • 国外web设计网站模板河南百度关键词优化排名软件
  • 南通做微网站网站快速排名优化
  • 本地南昌网站建设公司网站推广经验
  • 铜川泰士公馆建设网站自己创建一个网站需要多少钱
  • 网站建设公司简介范文seo搜索优化公司报价
  • 秦皇岛哪家做网站好哪家公司建设网站好
  • 网站建哪家好做网站公司哪家正规
  • dw网站二级页面怎么做动态网站设计毕业论文
  • 网站开发基本语言武汉最新消息今天
  • 适合企业网站的cms宁波seo排名外包
  • 做网站怎么收费的数字营销软件
  • 建设工程其它费计算网站苹果aso优化
  • 企业电话名单从哪里弄seo报告
  • 电子商务网站建设运营武汉网站推广