`
xangqun
  • 浏览: 79647 次
  • 性别: Icon_minigender_1
  • 来自: 江西
社区版块
存档分类
最新评论

模板模式java源码实现(大话设计模式学习备忘录)

阅读更多
public class TestPagerA {

	public void TestQuestion1(){
		System.out.println("当你在学校取得好成绩或者在公司取得优异成绩而受到老师的称赞时,你作何反应?A感谢B谦逊C不直接回答问题而微笑");
		System.out.println("答案:B");
	}
	public void TestQuestion2(){
		System.out.println("当你在学校取得好成绩或者在公司取得优异成绩而受到老师的称赞时,你作何反应?A感谢B谦逊C不直接回答问题而微笑");
		System.out.println("答案:B");
	}
	public void TestQuestion3(){
		System.out.println("当你在学校取得好成绩或者在公司取得优异成绩而受到老师的称赞时,你作何反应?A感谢B谦逊C不直接回答问题而微笑");
		System.out.println("答案:B");
	}
}


public class TestPagerB {

	public void TestQuestion1(){
		System.out.println("当你在学校取得好成绩或者在公司取得优异成绩而受到老师的称赞时,你作何反应?A感谢B谦逊C不直接回答问题而微笑");
		System.out.println("答案:A");
	}
	public void TestQuestion2(){
		System.out.println("当你在学校取得好成绩或者在公司取得优异成绩而受到老师的称赞时,你作何反应?A感谢B谦逊C不直接回答问题而微笑");
		System.out.println("答案:A");
	}
	public void TestQuestion3(){
		System.out.println("当你在学校取得好成绩或者在公司取得优异成绩而受到老师的称赞时,你作何反应?A感谢B谦逊C不直接回答问题而微笑");
		System.out.println("答案:A");
	}
}

/**
 * <h1>模板模式</h1>
 * QS:小学时,抄黑板上的题目
 * @author xangqun
 *
 */
public class Program {

	/**PS:
	 * 处理答案不同,没什么不一样这样写又容易出错,又难于维护
	 * @param args
	 */
	public static void main(String[] args) {

		System.out.println("学生a抄的试卷:");
		TestPagerA a=new TestPagerA();
		a.TestQuestion1();
		a.TestQuestion2();
		a.TestQuestion3();
		
		System.out.println("学生b抄的试卷:");
		TestPagerB b=new TestPagerB();
		b.TestQuestion1();
		b.TestQuestion2();
		b.TestQuestion3();
	}

}

public abstract class TestPager {

	public void TestQuestion1(){
		System.out.println("当你在学校取得好成绩或者在公司取得优异成绩而受到老师的称赞时,你作何反应?A感谢B谦逊C不直接回答问题而微笑");
		System.out.println("答案:"+Answer1());
	}
	public void TestQuestion2(){
		System.out.println("当你在学校取得好成绩或者在公司取得优异成绩而受到老师的称赞时,你作何反应?A感谢B谦逊C不直接回答问题而微笑");
		System.out.println("答案:"+Answer2());
	}
	public void TestQuestion3(){
		System.out.println("当你在学校取得好成绩或者在公司取得优异成绩而受到老师的称赞时,你作何反应?A感谢B谦逊C不直接回答问题而微笑");
		System.out.println("答案:"+Answer3());
	}
	public abstract String Answer1();
	public abstract String Answer2();
	public abstract String Answer3();
}

public class TestPagerAtwo extends TestPager {

	@Override
	public String Answer1() {
		return "a";
	}

	@Override
	public String Answer2() {
		return "a";
	}

	@Override
	public String Answer3() {
		return "a";
	}

}

public class TestPagerBtwo extends TestPager {

	@Override
	public String Answer1() {
		return "b";
	}

	@Override
	public String Answer2() {
		return "b";
	}

	@Override
	public String Answer3() {
		return "b";
	}

}

/**
 * <h1>模板模式</h1>
 * QS:小学时,抄黑板上的题目
 * @author xangqun
 *
 */
public class ProgramTwo {

	/**
	 * <b>模板模式</b>
	 * 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。
	 * 模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。
	 * <br>模板模式是通过把不变行为搬移到超类,去除子类中重复代码来体现它的优势
	 * 当不变的和可变的行为在方法的子类实现中混合在一起的时候,不变的行为就会在子类中重复出现,
	 * 我们通过模板方法模式把这些行为搬移到单一的地方,这样就帮助子类摆脱子类重复的不变行为的纠缠。
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("学生a抄的试卷:");
		TestPager a=new TestPagerAtwo();
		a.TestQuestion1();
		a.TestQuestion2();
		a.TestQuestion3();
		
		System.out.println("学生b抄的试卷:");
		TestPager b=new TestPagerBtwo();
		b.TestQuestion1();
		b.TestQuestion2();
		b.TestQuestion3();

	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics