模板方法定义:定义一个操作中算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以不改变一个算法的结构即可重定义该算法的特定步骤。
模板方法是一种行为型模式。
/**
 * 实现了一个模板方法,定义了算法的骨架。
 * 具体子类将重新定义primitiveMethod()以实现一个算法的步骤
 *
 * 注意:是抽象类,不是接口。
 * 为什么不是接口?因为templateMethod()方法有具体的方法体。
 */
public abstract class AbstractClass {
    public void templateMethod() {
        primitiveOperation1();
        primitiveOperation2();
        System.out.println("这里是算法骨架中的一些共用的操作");
    }
    protected abstract void primitiveOperation1();
    protected abstract void primitiveOperation2();
}/**
 * 具体类A
 */
public class ConcreteClassA extends AbstractClass {
    @Override
    protected void primitiveOperation1() {
        System.out.println("具体类A的方法1实现");
    }
    @Override
    protected void primitiveOperation2() {
        System.out.println("具体类A的方法2实现");
    }
}/**
 * 具体类B
 */
public class ConcreteClassB extends AbstractClass {
    @Override
    protected void primitiveOperation1() {
        System.out.println("具体类B的方法1实现");
    }
    @Override
    protected void primitiveOperation2() {
        System.out.println("具体类B的方法2实现");
    }
}客户端代码如下。
public class Client {
    public static void main(String[] args) {
        AbstractClass c;
        c = new ConcreteClassA();
        c.templateMethod();
        System.out.println("----------------");
        c = new ConcreteClassB();
        c.templateMethod();
    }
}
/**
 *
 输出如下:
 具体类A的方法1实现
 具体类A的方法2实现
 这里是算法骨架中的一些共用的操作
 ----------------
 具体类B的方法1实现
 具体类B的方法2实现
 这里是算法骨架中的一些共用的操作
 *//**
 * 测试试卷类,包含了题目和选项。
 * 学生仅仅填写答案就可以.
 */
public abstract class TestPaper {
    public void testQuestion1() {
        System.out.println("测试题目1: xxxxxx,下面答案正确的是。A.xxxx B.yyyy C.zzzz D.wwww");
        System.out.println("你的答案为:" + answer1());
    }
    public void testQuestion2() {
        System.out.println("测试题目2: xxxxxx,下面答案正确的是。A.xxxx B.yyyy C.zzzz D.wwww");
        System.out.println("你的答案为:" + answer2());
    }
    public void testQuestion3() {
        System.out.println("测试题目3: xxxxxx,下面答案正确的是。A.xxxx B.yyyy C.zzzz D.wwww");
        System.out.println("你的答案为:" + answer3());
    }
    protected abstract String answer1();
    protected abstract String answer2();
    protected abstract String answer3();
}/**
 * 学生A的试卷
 */
public class TestPaperA extends TestPaper {
    @Override
    protected String answer1() {
        return "A";
    }
    @Override
    protected String answer2() {
        return "A";
    }
    @Override
    protected String answer3() {
        return "A";
    }
}/**
 * 学生B的试卷
 */
public class TestPaperB extends TestPaper {
    @Override
    protected String answer1() {
        return "B";
    }
    @Override
    protected String answer2() {
        return "B";
    }
    @Override
    protected String answer3() {
        return "B";
    }
}public class Client {
    public static void main(String[] args) {
        TestPaper testPaperA = new TestPaperA();
        testPaperA.testQuestion1();
        testPaperA.testQuestion2();
        testPaperA.testQuestion3();
        System.out.println("-------------------------------------");
        TestPaper testPaperB = new TestPaperB();
        testPaperB.testQuestion1();
        testPaperB.testQuestion2();
        testPaperB.testQuestion3();
    }
}
/**
 *输出如下:
 测试题目1: xxxxxx,下面答案正确的是。A.xxxx B.yyyy C.zzzz D.wwww
 你的答案为:A
 测试题目2: xxxxxx,下面答案正确的是。A.xxxx B.yyyy C.zzzz D.wwww
 你的答案为:A
 测试题目3: xxxxxx,下面答案正确的是。A.xxxx B.yyyy C.zzzz D.wwww
 你的答案为:A
 -------------------------------------
 测试题目1: xxxxxx,下面答案正确的是。A.xxxx B.yyyy C.zzzz D.wwww
 你的答案为:B
 测试题目2: xxxxxx,下面答案正确的是。A.xxxx B.yyyy C.zzzz D.wwww
 你的答案为:B
 测试题目3: xxxxxx,下面答案正确的是。A.xxxx B.yyyy C.zzzz D.wwww
 你的答案为:B
 */原文:https://www.cnblogs.com/yeyang/p/10124356.html