📄 client.java
字号:
/*
* Created on 2005-5-11
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.test.pattern.behavir.template;
/**
* @author Administrator
*
* 意图 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。Te m p l a t e M e t h o d
* 使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。 适用性 一次性实现一个算法的不变的部分,并将可变的行为留给子类来实现。
* 各子类中公共的行为应被提取出来并集中到一个公共父类中以避免代码重复。这是O p d y k e 和J o h n s o n
* 所描述过的“重分解以一般化”的一个很好的例子[ O J 9 3
* ]。首先识别现有代码中的不同之处,并且将不同之处分离为新的操作。最后,用一个调用这些新的操作的模板方法来替换这些不同的代码。
* 控制子类扩展。模板方法只在特定点调用“h o o k ”操作(参见效果一节),这样就只允许在这些点进行扩展。
*/
class Algorithm {
public void DoAlgorithm() {
System.out.println("In DoAlgorithm");
// do some part of the algorithm here
// step1 goes here
System.out.println("In Algorithm - DoAlgoStep1");
// . . .
// step 2 goes here
System.out.println("In Algorithm - DoAlgoStep2");
// . . .
// Now call configurable/replacable part
DoAlgoStep3();
// step 4 goes here
System.out.println("In Algorithm - DoAlgoStep4");
// . . .
// Now call next configurable part
DoAlgoStep5();
}
public void DoAlgoStep3() {
System.out.println("In Algorithm - DoAlgoStep3");
}
public void DoAlgoStep5() {
System.out.println("In Algorithm - DoAlgoStep5");
}
}
class CustomAlgorithm extends Algorithm {
public void DoAlgoStep3() {
System.out.println("In CustomAlgorithm - DoAlgoStep3");
}
public void DoAlgoStep5() {
System.out.println("In CustomAlgorithm - DoAlgoStep5");
}
}
/// <summary>
/// Summary description for Client.
/// </summary>
public class Client {
public static void main(String[] args) {
CustomAlgorithm c = new CustomAlgorithm();
c.DoAlgorithm();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -