读书人

Template Method Pattern 模板方法方式

发布时间: 2012-09-10 11:02:32 作者: rapoo

Template Method Pattern 模板方法模式

1. 简介

?

在一些方法中定义一个算法的骨架,而将一些步骤延迟到子类中。

模板方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤。

?

2.类图


Template Method Pattern 模板方法方式
?

3.实例

?

--摘自Junit的TestCase源码

?

......public abstract class TestCase extends Assert implements Test {......                /** * Runs the bare test sequence. * @exception Throwable if any exception is thrown */public void runBare() throws Throwable {setUp();try {runTest();}finally {tearDown();}}/** * Sets up the fixture, for example, open a network connection. * This method is called before a test is executed. */protected void setUp() throws Exception {}/** * Tears down the fixture, for example, close a network connection. * This method is called after a test is executed. */protected void tearDown() throws Exception {}......}

?

读书人网 >软件架构设计

热点推荐