◎筱米加步枪◎.Blog

Happy coding

模版方法模式

筱米加步枪 posted @ 2010年3月22日 13:00 in [ 设计模式 ] with tags 设计模式 模板方法模式 , 1500 阅读

模版方法模式,简单来说就是封装了算法的骨架,将一些可变的部分延迟到之类中实现。

模版方法模式使得子类可以不改变一个算法的结构即可重新定义该算法的某些特定的步骤。

模版方法模式的结构图如下:

以一个例子来说明模版模式吧,也是参考网上总结的。

我们知道,两个人从认识到结婚,是要经历一些过程的,比如初次见面、吃饭、约会、婚礼等一系列过程,每个人都要经历这些阶段,但是每个人的这些过程实现都是不一样的,典型的特例,以穷人和富人,他们实施这些事情的时候,具体肯定是不一样的。请看代码:

1.婚恋的抽象类。【模版类】

/**
 * 一个【爱】的抽象类,描述了属于两个人婚恋的过程,
 * 从相遇到吃饭,到约会,最后到结婚
 * 
 * @author ChenST
 * 
 * @create 2010-3-22
 */
public abstract class Love {
	
	/**
	 * 两个人见面的过程
	 */
	public abstract void meet();
	
	/**
	 * 两个人吃饭的过程
	 */
	public abstract void dinner();
	
	/**
	 * 两个人约会的过程
	 */
	public abstract void appointment();
	
	/**
	 * 两个人结婚的过程
	 */
	public abstract void wedding();
	
	/**
	 * 两个人爱情的一个模版(模版方法)//final为了不被之类覆盖
	 */
	public final void loveTemplate(){
		if(this.doMarray()){
			this.meet();
			this.dinner();
			this.appointment();
			this.wedding();
		}
	}
	
	/**
	 * 两个人是否要结婚,是的话返回true,否则返回false (钩子方法)
	 * 可由子类来控制
	 * @return
	 */
	public boolean doMarray(){
		return true;
	}
}

2.【穷人】的婚恋过程

/**
 * 【穷人】的恋爱过程
 * 
 * @author ChenST
 * 
 * @create 2010-3-22
 */
public class PoolerLove extends Love {

	@Override
	public void appointment() {
		System.out.println("去爬免费的山,当作运动!");
	}

	@Override
	public void dinner() {
		System.out.println("去沙县吃扁肉拌面!");
	}

	@Override
	public void meet() {
		System.out.println("借了一套西装,与女孩见面!");
	}

	@Override
	public void wedding() {
		System.out.println("摆一桌酒,一家人吃的开开心心!");
	}
}

3.【富人】的婚恋过程

/**
 * 【富人】的婚恋过程
 * 
 * @author ChenST
 * 
 * @create 2010-3-22
 */
public class RicherLove extends Love{

	@Override
	public void appointment() {
		System.out.println("玩遍中国的大江南北,爱国!");
	}

	@Override
	public void dinner() {
		System.out.println("天天吃香格里拉!");
	}

	@Override
	public void meet() {
		System.out.println("穿名牌西装,约在香格里拉酒店见面!");
	}

	@Override
	public void wedding() {
		System.out.println("婚礼全国直播!");
	}
}

4.测试类

/**
 * 测试类
 * 
 * @author ChenST
 * 
 * @create 2010-3-22
 */
public class Test {
	
	public static void main(String[] args) {
		System.out.println("穷人恋爱过程");
		//穷人恋爱过程
		Love pooler=new PoolerLove();
		//调用恋爱模版
		pooler.loveTemplate();
		
		System.out.println("\n富人恋爱过程");
		//富人恋爱过程
		Love richer=new RicherLove();
		//调用恋爱模版
		richer.loveTemplate();
	}
}

5.运行结果:

穷人恋爱过程
借了一套西装,与女孩见面!
去沙县吃扁肉拌面!
去爬免费的山,当作运动!
摆一桌酒,一家人吃的开开心心!

富人恋爱过程
穿名牌西装,约在香格里拉酒店见面!
天天吃香格里拉!
玩遍中国的大江南北,爱国!
婚礼全国直播!

可以看到结果,模版方法的好处就是,你还可以根据自己的特点,实现自己的婚恋过程,只需调用恋爱模版即可~~

Rajshahi Board PSC R 说:
2022年9月01日 05:33

Bangladesh Education Board DPE has conducted the class 8th grade of Junior School Certificate Exam and Junior Dakhil Certificate Exam on 1st to 15th November 2022 at all centers in division wise under Ministry of Primary and Mass Education (MOPME), and the class 8th grade terminal examination tests are successfully conducted for all eligible JSC/JDC students for the academic year of 2022. Rajshahi Board PSC Result Full Marksheet The Bangladesh government Minister of Secondary Education is going to announce the JSC Result 2022 in student wise for division students in education board wise, and the result documents will be submitted to the Prime Minister of the country after then the result with mark sheet will be announced to public to check the individual result.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter