大话设计模式——07模板方法模式

一、两份试卷的问题

public class TestPaperA {

	public void question1() {
		System.out.println("第一题:... ...");
		System.out.println("答案:a");
	}

	public void question2() {
		System.out.println("第二题:... ...");
		System.out.println("答案:b");
	}

	public void question3() {
		System.out.println("第三题:... ...");
		System.out.println("答案:c");
	}
	
}
public class TestPaperB {

	public void question1() {
		System.out.println("第一题:... ...");
		System.out.println("答案:c");
	}

	public void question2() {
		System.out.println("第二题:... ...");
		System.out.println("答案:b");
	}

	public void question3() {
		System.out.println("第三题:... ...");
		System.out.println("答案:a");
	}
	
}
public class RunMain {

	public static void main(String[] args) {

		System.out.println("学生甲的试卷:");
		TestPaperA studentA = new TestPaperA();
		studentA.question1();
		studentA.question2();
		studentA.question3();
		
		System.out.println("学生乙的试卷:");
		TestPaperB studentB = new TestPaperB();
		studentB.question1();
		studentB.question2();
		studentB.question3();

	}

}

二、运用模板方法模式

// 既然用了继承,并且肯定这个继承有意义,就应该要成为子类的模板,所有重复的代码都应该要上升到父类去,而不是让每个子类都去重复。
// 当我们要完成在某一细节层次一致的一个过程或一系列步骤,但其个别步骤在更详细的层次上的实习可能不同时,我们通常考虑用模板方法模式来处理。
public class TestPaper {

	public void question1() {
		System.out.println("第一题:... ...");
		System.out.println("答案:" + answer1()); //改为一个虚方法(被子类重写的方法)
	}

	public void question2() {
		System.out.println("第二题:... ...");
		System.out.println("答案:" + answer2());
	}

	public void question3() {
		System.out.println("第三题:... ...");
		System.out.println("答案:" + answer3());
	}

	// 此方法的目的就是给继承的子类重写,因为这里每个人的答案都是不同的。
	protected String answer1(){
		return "";
	}
	protected String answer2(){
		return "";
	}
	protected String answer3(){
		return "";
	}
	
}
public class TestPaperA extends TestPaper {

	@Override
	protected String answer1(){
		return "A";
	}
	
	@Override
	protected String answer2(){
		return "B";
	}
	
	@Override
	protected String answer3(){
		return "C";
	}
	
}
public class TestPaperB extends TestPaper {

	@Override
	protected String answer1(){
		return "C";
	}
	
	@Override
	protected String answer2(){
		return "B";
	}
	
	@Override
	protected String answer3(){
		return "A";
	}
	
}
// 模板方法模式提供了一个很好的代码复用平台,把不变行为转移到超类,去除子类中的重复代码。
// 当不变的和可变的行为在方法的子类实现中混合在一起的时候,不变的行为就会在子类中重复出现。我们可以通过模板方法模式把这些行为转移到单一的地方,这样就可以去除子类中重复的不变行为。
public class RunMain {

	public static void main(String[] args) {

		System.out.println("学生甲的试卷:");
		TestPaper studentA = new TestPaperA(); //将子类变量的声明改成父类,利用多态性,实现代码的复用。
		studentA.question1();
		studentA.question2();
		studentA.question3();
		
		System.out.println("学生乙的试卷:");
		TestPaper studentB = new TestPaperB();
		studentB.question1();
		studentB.question2();
		studentB.question3();

	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值