23种设计模式----模板方法模式----行为模式

版权声明:本文为博主原创文章,转载请注明作者。 https://blog.csdn.net/a18792721831/article/details/83095565

1.模板方法模式是什么

通常情况下,我们做事情有一个模板就比较好做,因为有模板可以使用。甚至在写字的时候,有一本字帖,我们只要对应着写就行。
所以,模板方法就是定义处理一系列问题的解决方式的集合。
可以这么理解,我有4个类,每个类都需要调用其中的4个方法,那么。就可以使用模板方法,代替手工调用这些方法。

2.模板方法的组成

模板方法主要有2种类组成:
模板类
实现类

3.模板方法中不同的类的行为

模板类:
定义实现类或其他类的行为(方法调用的顺序)
实现类:
具体实现模板类中调用的未实现的方法或者需要重写的方法

4.例子

4.1 例子的背景

父亲有好几个孩子,现在父亲老了,需要寻找继承人,于是。。。。

4.2模板类–父亲寻找继承人的方式

import java.text.SimpleDateFormat;
import java.util.Date;

public abstract class Father {
	
	private String name;
	
	private Integer age;
	
	public Father(String name,Integer age){
		this.name = name;
		this.age = age;
	}
	
	//演讲
	abstract protected void lecture();
	//唱歌
	abstract protected void sing();
	//自我介绍
	protected void selfIntroduction(){
		System.out.println("我叫"+this.name+",我今年"+this.age+"岁了!");
	}
	//父类定义的行为或者方法调用
	public void examineChild(){
		System.out.println("examineChild******************start*******************");
		System.out.println();
		System.out.println();
		System.out.println("今天是"+new SimpleDateFormat("yyyy-mm-dd HH:mm:ss").format(new Date())+"今天天气不错!");
		selfIntroduction();
		lecture();
		sing();
		System.out.println();
		System.out.println();
		System.out.println("examineChild******************end*******************");
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	
}

父亲定义了一系列的行为,其中唱歌和演讲需要孩子类必须实现,而自我介绍方法,如果孩子类没有实现,那么父亲类也有默认的方法可以调用。

4.3实现类----孩子类

public class FirstChild extends Father{

	public FirstChild(String name, Integer age) {
		super(name, age);
	}

	@Override
	protected void lecture() {
		System.out.println("lecture******************start*******************");
		System.out.println("我是大儿子,我一生下来就收到父亲莫大的喜爱。");
		System.out.println("lecture******************end*******************");
	}

	@Override
	protected void sing() {
		System.out.println("sing******************start*******************");
		System.out.println("我不怎么会唱歌,但是我依稀记得父亲唱过的歌曲很好听。");
		System.out.println("sing******************end*******************");
	}
	
	protected void selfIntroduction(){
		System.out.println("selfIntroduction******************start*******************");
		super.selfIntroduction();
		System.out.println("我是父亲第一个孩子,我一定会做好自己。");
		System.out.println("selfIntroduction******************end*******************");
	}
}

public class SecendChild extends Father{

	public SecendChild(String name, Integer age) {
		super(name, age);
	}

	@Override
	protected void lecture() {
		System.out.println("lecture******************start*******************");
		System.out.println("作为父亲第二个孩子,得到的关爱很少,需要完成的事情却很多");
		System.out.println("lecture******************end*******************");
	}

	@Override
	protected void sing() {
		System.out.println("sing******************start*******************");
		System.out.println("父亲给大哥唱歌时,我也在旁边听过。");
		System.out.println("sing******************end*******************");
	}
	
	protected void selfIntroduction(){
		System.out.println("selfIntroduction******************start*******************");
		super.selfIntroduction();
		System.out.println("我是父亲的第二个孩子,我从小就帮父亲的忙。");
		System.out.println("selfIntroduction******************end*******************");
	}
	
}

public class ThridChild extends Father{

	public ThridChild(String name, Integer age) {
		super(name, age);
	}

	@Override
	protected void lecture() {
		System.out.println("lecture******************start*******************");
		System.out.println("二姐帮了父亲的大忙,但是父亲却不怎么喜欢二姐。");
		System.out.println("lecture******************end*******************");
	}

	@Override
	protected void sing() {
		System.out.println("sing******************start*******************");
		System.out.println("小学时的儿童节,父亲听了我唱的歌");
		System.out.println("sing******************end*******************");
	}
	
	protected void selfIntroduction(){
		System.out.println("selfIntroduction******************start*******************");
		super.selfIntroduction();
		System.out.println("我是父亲第三个孩子,我能感受到父亲有很大的压力。");
		System.out.println("selfIntroduction******************end*******************");
	}

}

public class FourthChild extends Father{

	public FourthChild(String name, Integer age) {
		super(name, age);
	}

	@Override
	protected void lecture() {
		System.out.println("lecture******************start*******************");
		System.out.println("在我的记忆中,父亲很少陪我们,我对父亲很生疏。");
		System.out.println("lecture******************end*******************");
	}

	@Override
	protected void sing() {
		System.out.println("sing******************start*******************");
		System.out.println("我小时候天天给父亲唱歌,父亲也从来没认真听过。");
		System.out.println("sing******************end*******************");
	}

	protected void selfIntroduction(){
		System.out.println("selfIntroduction******************start*******************");
		super.selfIntroduction();
		System.out.println("我是父亲最小的孩子,我喜欢母亲胜过父亲。");
		System.out.println("selfIntroduction******************end*******************");
	}
}

4.4测试类----Main

public class Main {

	public static void main(String[] args) {

		//创建4个孩子
		FirstChild DaNiu = new FirstChild("大牛", 38);
		SecendChild liLi = new SecendChild("丽丽", 35);
		ThridChild Qiang = new ThridChild("强子", 33);
		FourthChild XiaoJun = new FourthChild("小军", 30);
		DaNiu.examineChild();
		liLi.examineChild();
		Qiang.examineChild();
		XiaoJun.examineChild();
		
	}

}

4.5效果

examineChild******************start*******************


今天是2018-23-16 18:23:22今天天气不错!
selfIntroduction******************start*******************
我叫大牛,我今年38岁了!
我是父亲第一个孩子,我一定会做好自己。
selfIntroduction******************end*******************
lecture******************start*******************
我是大儿子,我一生下来就收到父亲莫大的喜爱。
lecture******************end*******************
sing******************start*******************
我不怎么会唱歌,但是我依稀记得父亲唱过的歌曲很好听。
sing******************end*******************


examineChild******************end*******************
examineChild******************start*******************


今天是2018-23-16 18:23:22今天天气不错!
selfIntroduction******************start*******************
我叫丽丽,我今年35岁了!
我是父亲的第二个孩子,我从小就帮父亲的忙。
selfIntroduction******************end*******************
lecture******************start*******************
作为父亲第二个孩子,得到的关爱很少,需要完成的事情却很多
lecture******************end*******************
sing******************start*******************
父亲给大哥唱歌时,我也在旁边听过。
sing******************end*******************


examineChild******************end*******************
examineChild******************start*******************


今天是2018-23-16 18:23:22今天天气不错!
selfIntroduction******************start*******************
我叫强子,我今年33岁了!
我是父亲第三个孩子,我能感受到父亲有很大的压力。
selfIntroduction******************end*******************
lecture******************start*******************
二姐帮了父亲的大忙,但是父亲却不怎么喜欢二姐。
lecture******************end*******************
sing******************start*******************
小学时的儿童节,父亲听了我唱的歌
sing******************end*******************


examineChild******************end*******************
examineChild******************start*******************


今天是2018-23-16 18:23:22今天天气不错!
selfIntroduction******************start*******************
我叫小军,我今年30岁了!
我是父亲最小的孩子,我喜欢母亲胜过父亲。
selfIntroduction******************end*******************
lecture******************start*******************
在我的记忆中,父亲很少陪我们,我对父亲很生疏。
lecture******************end*******************
sing******************start*******************
我小时候天天给父亲唱歌,父亲也从来没认真听过。
sing******************end*******************


examineChild******************end*******************

5.使用模板方法模式的优点

1.在父类定义好具体的行为,不关系具体的实现,只关心较大一点的逻辑。
2.定义子类必须实现的方法
3.给子类方法增加默认的操作
4.未实现的功能。

猜你喜欢

转载自blog.csdn.net/a18792721831/article/details/83095565