模板模式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();
}
} 相关推荐
Balmunc 2020-08-02
hegaoye0 2020-08-01
gaochujia 2020-05-31
masternan 2020-04-10
trandy 2016-12-20
droidpioneer的IT 2013-12-29
Darklovy 2019-11-03
yishujixiaoxiao 2019-10-20
89251348 2019-09-23
jscese 2017-11-15
宁静致远 2010-12-24
masternan 2019-07-01
xusong 2013-06-26
chengrile 2013-06-01
TiDBPingCAP 2010-04-02
finalcola 2010-04-02
bobpipi 2010-04-02