Life Design Patterns of template pattern


Before talking about the principle of template mode, in accordance with our management, first to a life example. Teahouse need to develop an automatic tea and coffee program.

This article comes from: a template mode "Kaige learning design patterns" series of tutorial

Let's look at coffee and tea are the steps:

document_image_rId9.png

According to the map we write code to achieve:

Coffee type, as shown below:

document_image_rId10.png

FIG tea classes are as follows:

document_image_rId11.png

Test categories:

document_image_rId12.png

operation result:

document_image_rId13.png

Very simple. It is also very easy to write. Written very clearly.

From the above figure can be found, two processes are almost the same routine (step). Among them, the same components: water to a boil, pour a cup, gave the guests. These three steps are the same.

Changes are: red bubble tea or coffee; sugar / milk or lemon these two steps is changed.

The first version of the project evolution:

We will change the extracted out into a common class. HotDrink . Then let coffe and tea inherit common class. FIG class obtained as follows:

document_image_rId14.png

hotdrink superclass code is as follows:

document_image_rId15.png

The second version of the project evolution:

After analysis, we found that the two processes as well as the same place:

Step 1. The two processes are the same (all five steps);

2. Whether brew tea or coffee are operating;

3. Whether all add sugar or lemon seasoning.

So, once we extract of projects in progress:

We will also extract the operation flow superclass, 2 and 3 the operation is placed in the superclass. Let subclass implementation. Therefore class diagram obtained as follows:

document_image_rId16.png

We take a look inside this hotdrink categories:

public abstract class HotDrink {

   public final void prepareRecipe(){

       boilWater();

       brew();

       pourInCup();

       addCondiments();

       send();


   }


   protected abstract void addCondiments();


   protected abstract void brew();


   private final void boilWater() {

       System.out.println("一.烧水");

   }

   private final void pourInCup() {

       System.out.println("三.倒入杯中");

   }

document_image_rId17.png

我们发现,在prepareRecipe方法和boilWter、pourInCup、send这四个操作都添加了final关键字来修改。这是为什么呢?

从上面分析,我们知道,都是五个步骤,而且五个步骤中的三个步骤(烧水、倒入杯中、送客人)也是固定不变的。那么,在Java中,固定不变的这个怎么表示呢?对了,就使用fianl这个关键字修饰就可以了。这样,就可以放置子类不能随便修改步骤(比如由五步变成三步),已经规定的不能在修改了。比如烧水这个不烧了,这样是不行的。

我们来看看,热饮coffee和tea的类:

document_image_rId18.png

hotDrinkTea:

document_image_rId19.png

测试方法:

document_image_rId20.png

运行结果:

document_image_rId21.png

我们对项目进化进行复盘总结,可以得到:

所谓的模板模式:封装了一个算法的步骤,并允许子类为一个或多个步骤方法提供实现。模板模式,可以使子类在不改变算法结构(如上面的五步)的情况下,重新定义算法中某些步骤(如上面的第二步和第四步)

模板模式类图如下:

document_image_rId22.png

类图说明:

1:是一个抽象类(如:hotDrink)

2:有个模板方法。这个模板方法是final的(如:prepareRecipe方法)

3:由三种方法:

AbsOperation:抽象的方法(泡咖啡、加牛奶)

concreteOp: specific methods (e.g., boiling water may or may not be final.)

hook: hook. May be selected from a subclass can override the parent class.

Let's demonstrate with a hook under the.

For example, now has a new demand, customers may choose or need to add seasoning. This how to do it?

This article Source:

Kaige Java (kaigejava)

Kaige personal blog: www.kaigejava.com


We re-define templates:

document_image_rId23.png

tea implements this template class, and not with lemon:

document_image_rId24.png

Test run:

document_image_rId25.png

result:

document_image_rId26.png




Guess you like

Origin blog.51cto.com/kaigejava/2435671