Template
定義
:
定義一個操作中演算法的骨架
,
將一些步驟的執行延遲到其子類別中。
其實
Java
的抽象類別本來就是
Template
模式
,
因此使用很普遍。而且很容易理解和使用
,
我們直接以示例開始
:
public abstract class Benchmark
{
/**
*
下面操作是我們希望在子類別中完成
*/
public abstract void benchmark();
/**
*
重復執行
benchmark
次數
*/
public final long repeat (int count) {
if (count <= 0)
return 0;
else {
long startTime = System.currentTimeMillis();
for (int i = 0; i < count; i++)
benchmark();
long stopTime = System.currentTimeMillis();
return stopTime - startTime;
}
}
}
|
在上例中
,
我們希望重復執行
benchmark()
操作
,
但是對
benchmark()
的具體內容沒有說明
,
而是延遲到其子類別中描述
:
public class MethodBenchmark extends Benchmark
{
/**
*
真正定義
benchmark
內容
*/
public void benchmark() {
for (int i = 0; i < Integer.MAX_VALUE; i++){
System.out.printtln("i="+i);
}
}
}
|
至此
,Template
模式已經完成
,
是不是很簡單
?
看看如何使用
:
Benchmark operation = new MethodBenchmark();
long duration = operation.repeat(Integer.parseInt(args[0].trim()));
System.out.println("The operation took " + duration + " milliseconds");
也許你以前還疑惑抽象類別有什麽用
,
現在你應該徹底明白了吧
?
至於這樣做的好處
,
很顯然啊
,
擴展性強
,
以後
Benchmark
內容變化
,
我只要再做一個繼承子類別就可以
,
不必修改其他應用代碼。
分享到:
相关推荐
设计模式之Template 设计模式之Memento(备忘机制) 设计模式之Observer 设计模式之Chain of Responsibility(职责链) 设计模式之Command 设计模式之State 设计模式之Strategy(策略) 设计模式之Mediator(中介者) 设计...
设计模式之Template 设计模式之Memento(备忘机制) 设计模式之Observer 设计模式之Chain of Responsibility(职责链) 设计模式之Command 设计模式之State 设计模式之Strategy(策略) 设计模式之Mediator(中介者) 设计...
详细介绍GoF设计模式以及应用... 创建模式:设计模式之Factory,设计模式之Prototype(原型),设计模式之Builder,设计模式之Singleton(单态)....行为模式:设计模式之Template,设计模式之Memento(备忘机制).................
主要介绍了C#设计模式之Template模板方法模式实现ASP.NET自定义控件 密码强度检测功能,简单介绍了模板方法模式的定义、原理及检测密码强度的相关使用技巧,需要的朋友可以参考下
Head First 设计模式 (八) 模板方法模式(Template Method pattern) C++实现
设计模式之 Template(模板方法) 实际上向你介绍了为什么要使用 Java 抽象类,该模式原理简单,使用很普遍. 设计模式之 Strategy(策略) 不同算法各自封装,用户端可随意挑选需要的算法. 设计模式之 Chain of ...
设计模式C++学习之模板方法模式(Template Method)
走进设计模式之—— Template(模板模式)
设计模式中的策略模式,template模式详细讲解。
本篇详细总结软件开发所遵循的MVC (Model-View-Controller, 模型-视图-控制器) 设计模式以及Django的MVT设计模式(Model-View-Template)如何遵循这种设计理念。Django Model(模型), URL(链接), View(视图) 和Template...
《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...
设计模式体现的是一种思想,而思想则是指导行为的一切,理解和掌握了设计模式,并不是说记住了23种(或更多)设计场景和解决策略(实际上这也是很重要的一笔财富),实际接受的是一种思想的熏陶和洗礼,等这种思想...
模板方法(TemplateMethod) 用意:定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。
第1章到第11章陆续介绍的设计模式为Strategy、Observer、Decorator、AbstractFactory、FactoryMethod、Singleton,Command、Adapter、Facade、TemplateMethod、Iterator、Composite、State、Proxy。最后三章比较特别...
第1章到第11章陆续介绍的设计模式为Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton,Command、Adapter、Facade、TemplateMethod、Iterator、Composite、State、Proxy。最后三章比较...
第1章到第11章陆续介绍的设计模式为Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton,Command、Adapter、Facade、TemplateMethod、Iterator、Composite、State、Proxy。最后三章比较...
设计模式精解-GoF 23种设计模式解析附C++实现源码 AbstractFactory模式、Adapater模式、Composite模式、Decorator模式、Factory模式、Observer模式、Strategy模式、Template模式
第1章到第11章陆续介绍的设计模式为Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton,Command、Adapter、Facade、TemplateMethod、Iterator、Composite、State、Proxy。最后三章比较...
设计模式(21)-Template Method Pattern 204 一、 模板方法(Template Method)模式 204 二、 模版方法模式的结构 204 三、 模板方法模式的示意性代码 205 四、 继承作为复用的工具 207 五、 一个实际应用模板方法...
设计模式精解-GoF 23 种设计模式解析附 C++实现源码 目 录 引 言 0.1 设计模式解析(总序) 0.2 设计模式解析后记 0.3 与作者联系 1 创建型模式 1.1 Factory模式 1.2 AbstactFactory模式 1.3 Singleton...