设计模式学习笔记(32)——模板方法模式实现

模板方法模式
一、重复=易错+难改
在这里插入图片描述

package operation;
//学生甲抄的试卷
class TestPaperA{
	//试题一
	public void TestQuestion1() {
		System.out.print("杨过得到,后来给了郭靖,练成倚天剑、屠龙刀的玄铁可能是【】a.球磨铸铁  b.马口铁  c.高速合金钢  d.碳素纤维");
		System.out.print("答案:b");
	}
	//试题二
		public void TestQuestion2() {
			System.out.print("杨过、程英、陆无双铲除了情花,造成【】a.使这种植物不再害人  b.使一种珍稀物种灭绝  c.破坏了那个生物圈的生态平衡  d.造成该地区沙漠化");
			System.out.print("答案:a");
		}
		//试题三
				public void TestQuestion3() {
					System.out.print("蓝凤凰致使华山师徒、桃谷六仙呕吐不止,如果你是大夫,会给他们开什么药【】a.阿司匹林  b.牛黄解毒片  c.氟哌酸  d.让他们喝大量牛奶  e.以上全不对");
					System.out.print("答案:c");
				}
}
//学生乙抄的试卷
class TestPaperB{
	//试题一
	public void TestQuestion1() {
		System.out.print("杨过得到,后来给了郭靖,练成倚天剑、屠龙刀的玄铁可能是【】a.球磨铸铁  b.马口铁  c.高速合金钢  d.碳素纤维");
		System.out.print("答案:d");
	}
	//试题二
		public void TestQuestion2() {
			System.out.print("杨过、程英、陆无双铲除了情花,造成【】a.使这种植物不再害人  b.使一种珍稀物种灭绝  c.破坏了那个生物圈的生态平衡  d.造成该地区沙漠化");
			System.out.print("答案:b");
		}
		//试题三
				public void TestQuestion3() {
					System.out.print("蓝凤凰致使华山师徒、桃谷六仙呕吐不止,如果你是大夫,会给他们开什么药【】a.阿司匹林  b.牛黄解毒片  c.氟哌酸  d.让他们喝大量牛奶  e.以上全不对");
					System.out.print("答案:a");
				}
}
public  class Main{
	public static void main(String[] args){
		System.out.println("学生甲抄的试卷:");
		TestPaperA studentA=new TestPaperA();
		studentA.TestQuestion1();
		studentA.TestQuestion2();
		studentA.TestQuestion3();
		System.out.println("学生乙抄的试卷:");
		TestPaperB studentB=new TestPaperB();
		studentB.TestQuestion1();
		studentB.TestQuestion2();
		studentB.TestQuestion3();
	}
}

二、提炼代码
老师出一份试卷,打印多份,让学生填答案就可以了。

package operation;
//金庸小说考题试卷

class TestPaper{
	//试题一
	public void TestQuestion1() {
		System.out.print("杨过得到,后来给了郭靖,练成倚天剑、屠龙刀的玄铁可能是【】a.球磨铸铁  b.马口铁  c.高速合金钢  d.碳素纤维");
		
	}
	//试题二
		public void TestQuestion2() {
			System.out.print("杨过、程英、陆无双铲除了情花,造成【】a.使这种植物不再害人  b.使一种珍稀物种灭绝  c.破坏了那个生物圈的生态平衡  d.造成该地区沙漠化");
			
		}
		//试题三
				public void TestQuestion3() {
					System.out.print("蓝凤凰致使华山师徒、桃谷六仙呕吐不止,如果你是大夫,会给他们开什么药【】a.阿司匹林  b.牛黄解毒片  c.氟哌酸  d.让他们喝大量牛奶  e.以上全不对");
					
				}
}
//学生甲抄的试卷
class TestPaperA extends TestPaper{
	
	
	public  void TestQuestion1() {
		
		System.out.print("答案:b");
	}
	
	public  void TestQuestion2() {
		
		
		System.out.print("答案:b");
	}
	
	public  void TestQuestion3() {
		
	
		System.out.print("答案:b");
	}
}
class TestPaperB extends TestPaper{
	
	
	public  void TestQuestion1() {
		
		
		System.out.print("答案:b");
	}
	
	public  void TestQuestion2() {
		
		
		System.out.print("答案:b");
	}
	
	public  void TestQuestion3() {
		
		System.out.print("答案:b");
	}
}
public  class Main{
	public static void main(String[] args){
		System.out.print("老师出的试卷:");
		TestPaper testpaper=new TestPaper();
		testpaper.TestQuestion1();
		testpaper.TestQuestion2();
		testpaper.TestQuestion3();
		System.out.println("学生甲抄的试卷:");
		TestPaperA studentA=new TestPaperA();
		studentA.TestQuestion1();
		studentA.TestQuestion2();
		studentA.TestQuestion3();
		System.out.println("学生乙抄的试卷:");
		TestPaperB studentB=new TestPaperB();
		studentB.TestQuestion1();
		studentB.TestQuestion2();
		studentB.TestQuestion3();
	}
}

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

package operation;
//金庸小说考题试卷

class TestPaper{
	//试题一
	public void TestQuestion1() {
		System.out.print("杨过得到,后来给了郭靖,练成倚天剑、屠龙刀的玄铁可能是【】a.球磨铸铁  b.马口铁  c.高速合金钢  d.碳素纤维");
		System.out.print("答案:"+Answer1());
	}
	//试题二
		public void TestQuestion2() {
			System.out.print("杨过、程英、陆无双铲除了情花,造成【】a.使这种植物不再害人  b.使一种珍稀物种灭绝  c.破坏了那个生物圈的生态平衡  d.造成该地区沙漠化");
			System.out.print("答案:"+Answer2());
		}
		//试题三
				public void TestQuestion3() {
					System.out.print("蓝凤凰致使华山师徒、桃谷六仙呕吐不止,如果你是大夫,会给他们开什么药【】a.阿司匹林  b.牛黄解毒片  c.氟哌酸  d.让他们喝大量牛奶  e.以上全不对");
					System.out.print("答案:"+Answer3());
				}
				protected String Answer1() {
					return"";
				}
				protected String Answer2() {
					return"";
				}
				protected String Answer3() {
					return"";
				}
}
//学生甲抄的试卷
class TestPaperA extends TestPaper{
	protected String Answer1() {
		return "b";
	}
	protected String Answer2() {
		return "c";
	}
	protected String Answer3() {
		return "a";
	}
}
class TestPaperB extends TestPaper{
	protected String Answer1() {
		return "c";
	}
	protected String Answer2() {
		return "a";
	}
	protected String Answer3() {
		return "a";
	}
}
public  class Main{
	public static void main(String[] args){
		System.out.println("学生甲的试卷:");
		TestPaper studentA=new TestPaperA();
		studentA.TestQuestion1();
		studentA.TestQuestion2();
		studentA.TestQuestion3();
		
		System.out.println("学生乙的试卷:");
		TestPaper studentB=new TestPaperB();
		studentB.TestQuestion1();
		studentB.TestQuestion2();
		studentB.TestQuestion3();
	}
}

三、模板方法模式
在这里插入图片描述

abstract class AbstractClass{
	public abstract void PrimitiveOperation1();
	public abstract void PrimitiveOperation2();
	public void TemplateMethod() {
		PrimitiveOperation1();
		PrimitiveOperation2();
		System.out.println("");
	}
}
class ConcreteClassA extends AbstractClass{
	public void PrimitiveOperation1() {
		System.out.println("具体类A方法1实现");
	}
	public void PrimitiveOperation2() {
		System.out.println("具体类A方法2实现");
	}
}
class ConcreteClassB extends AbstractClass{
	public void PrimitiveOperation1() {
		System.out.println("具体类B方法1实现");
	}
	public void PrimitiveOperation2() {
		System.out.println("具体类B方法2实现");
	}
}
public  class Main{
	public static void main(String[] args){
		AbstractClass c;
		c=new ConcreteClassA();
		c.TemplateMethod();
		c=new ConcreteClassB();
		c.TemplateMethod();
	}
}

模板方法模式就是提供了一个很好的代码复用平台。当不变的和可变的行为在方法的子类现实中混合在一起的时候,不变的行为就会在子类中重复出现。我们通过模板方法模式把这些行为搬到单一的地方,这样就帮助子类摆脱重复的不变行为的纠缠。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值