通过使用抽象类等,定义一个算法骨架,而将一些步骤延迟到子类中,使子类可以不改变一个算法的结构即可重新定义该算法的特定步骤.
适用场合:要完成某一细节层次一致的一个过程或一系列步骤,但其个别步骤在更详细的层次上的实现可能不同.
Template.java
package com.gary.designpattern.template;
/**
* 抽象模板
* @author gary
*
*/
public abstract class Template {
/**
* 模板方法,算法骨架
*/
public void sayHello(){
System.out.println("Hello" + getType());
}
/**
* 在子类中实现
* @return
*/
public abstract String getType();
}
HelloA.java
package com.gary.designpattern.template;
public class HelloA extends Template {
/**
* 实现
*/
@Override
public String getType() {
return "A";
}
}
HelloB.java
package com.gary.designpattern.template;
public class HelloB extends Template {
/**
* 实现
*/
@Override
public String getType() {
return "B";
}
}
Test.java
package com.gary.designpattern.template;
/**
* 测试
* @author gary
*
*/
public class Test {
public static void main(String[] args) {
HelloA helloA = new HelloA();
helloA.sayHello();
System.out.println("=========split=======");
HelloB helloB = new HelloB();
helloB.sayHello();
}
}
分享到:
相关推荐
设计模式中的策略模式,template模式详细讲解。
23种设计模式之十三(行为模式)Template模式
设计模式体现的是一种思想,而思想则是指导行为的一切,理解和掌握了设计模式,并不是说记住了23种(或更多)设计场景和解决策略(实际上这也是很重要的一笔财富),实际接受的是一种思想的熏陶和洗礼,等这种思想...
主要介绍了Java设计模式之模板模式(Template模式)介绍,定义一个操作中算法的骨架,将一些步骤的执行延迟到其子类中,需要的朋友可以参考下
3.1 Template模式 3.2 Strategy模式 3.3 State模式 3.4 Observer模式 3.5 Memento模式 3.6 Mediator模式 3.7 Command模式 3.8 Visitor模式 3.9 Chain of Responsibility模式 3.10 Iterator模式 3.11 Interpreter...
Eclipse Template与模式.doc
走进设计模式之—— Template(模板模式)
模板匹配器 模式匹配提取到单独的模块中安装npm install template-matcher --save用法 var matcher = require ( 'template-matcher' ) ;var matches = matcher ( '<div><input type="${attr}"/>${content}...
3.1 Template模式....................................................................................................................55 3.2 Strategy模式...................................................
3.1 Template模式....................................................................................................................74 3.2 Strategy模式 ...................................................
3.1 Template模式....................................................................................................................74 3.2 Strategy模式 ..................................................
3.1 Template模式..............................55 3.2 Strategy模式...............................59 3.3 State模式.....................................63 3.4 Observer模式................................
3.1 Template模式....................................................................................................................55 3.2 Strategy模式....................................................
3.1 Template模式............................................ 3.2 Strategy模式............................................. 3.3 State模式................................................... 3.4 Observer...
设计模式精解-GoF 23种设计模式解析附C++实现源码 AbstractFactory模式、Adapater模式、Composite模式、Decorator模式、Factory模式、Observer模式、Strategy模式、Template模式
3.1 Template模式....................................................................................................................55 3.2 Strategy模式 ..................................................