现在的位置: 首页 > 综合 > 正文

[ 设计模式 ] 装饰模式!

2014年06月27日 ⁄ 综合 ⁄ 共 3157字 ⁄ 字号 评论关闭

装饰模式:最简单的理解就是在你出门前,你需要对身先修饰一下,比如弄点胭脂,水粉啊,弄点好看的衣服啊。这就是装饰模式。

那么装饰模式用在什么地方呢。

可能很多人会觉得其实继承也能达到那样的效果。那么如果要我要装饰很多东西呢。什么耳环啊,什么胭脂啊,什么衣服啊,什么什么的。而且还是不一定每次都要这么多。

那你就去继承N个继承类么?这里主要是有存在装饰顺序问题,还是虽然装饰条件一样,但是可能某些需要装饰,某些条件不需要装饰。

如果用继承,那么就需要将所有的装饰条件都列举出来。比如5个条件吧。

那么装饰的可能就有:1,2,3,4,5,12,13,14,15,21,23,24,25......等等,很多。

但是用装饰就不同了。只需要5个条件。你可以去随意的组合。

那么这个时候装饰模式就来了。

首先看个例子。

你考试不及格。要去给家长看。那么正常你如果直接给你老爸看。那么我想一顿暴打不是可少的吧。至少是一顿痛骂吧。

那么现在看下成绩单,首先你得有一个试卷成绩接口吧:

package com.design.decorator;

/** 
 * @ClassName:ISchoolreport 
 * @Description:试卷成绩接口。
 * @author <helloyangzhi@foxmail.com>
 * @date 2012-12-15 上午10:09:16 
 * @version V1.0
 */

public interface ISchoolreport{
	
	void report();
}

 

实现类:

package com.design.decorator;

/** 
 * @ClassName: FourthSchoolReport 
 * @Description:实现类。
 * @author <helloyangzhi@foxmail.com>
 * @date 2012-12-15 上午10:12:13 
 * @version V1.0
 */

public class FourthSchoolReport  implements ISchoolreport{

	/* (非 Javadoc) 
	 * <p>Title: report  
	 * <p>Description:   
	 * @see com.design.decorator.ISchoolreport#report() 
	 */ 
	@Override
	public void report() {
		System.out.println("成绩50");
	}

	/* (非 Javadoc) 
	 * <p>Title: sign  
	 * <p>Description:   
	 * @see com.design.decorator.ISchoolreport#sign() 
	 */ 
	@Override
	public void sign(String name) {
		System.out.println(name);
	}
	

}

好吧如果就这样去给别人看,肯定是不行的。

所有这样咱先修饰修饰:

来一个修饰类:

package com.design.decorator;

/** 
 * @ClassName: Decorator 
 * @Description:装饰类,这里必须要去继承 FourthSchoolReport 类或者实现 ISchoolreport接口.
 * 这样才能实现,被装饰过的类,还能再一次的被装饰。 主要为了保持装饰类和被装饰类来自同一个接口。
 * @author <helloyangzhi@foxmail.com>
 * @date 2012-12-15 上午10:14:09 
 * @version V1.0
 */

public class Decorator implements ISchoolreport{
	
	
	//成绩类
	private ISchoolreport report;
	
	/**
	 *@Description:构造方法。获取被修饰类。当然被修饰过的类也可以做为修饰类。这就是为什么Decorator要去继承修饰类或接口。
	 */
	public Decorator(ISchoolreport report){
		this.report = report;
	}
	
	public void report(){
		this.report.report();
	}
	public void sign(String name){
		this.report.sign(name);
	}

}

当然这只是一个父类。那么你具体想怎么样去修饰。就看个人能力了。毕竟每个人的能力不一样嘛。

看第一个孩子的修饰:

/** 
 * @ClassName: ScoreDecorator 
 * @Description:成绩装饰类,Decorator的子类。重写父类的方法。对被修饰类作出特定的修饰。
 * @author <helloyangzhi@foxmail.com>
 * @date 2012-12-15 上午10:15:53 
 * @version V1.0
 */

public class ScoreDecorator extends Decorator{

	/** 
	 * <p>Title: </p> 
	 * <p>Description:  </p> 
	 * @param report 
	 */
	public ScoreDecorator(ISchoolreport report) {
		super(report);
	}
	
	
	/**
	 * @Description:修饰方法。
	 * */
	public void showReport(){
		System.out.println("总分就60分");
	}
	
	/**
	 * @Description:重写父类方法。添加自身修饰。
	 * **/
	public void report(){
		this.showReport();
		super.report();
	}
}

这孩子就说总分就60分。那么别人看了肯定觉得你很不错了!对不。

 

看另外一个的修饰:

package com.design.decorator;

/** 
 * @ClassName: SortDecorator 
 * @Description:排名修饰类。
 * @author <helloyangzhi@foxmail.com>
 * @date 2012-12-15 上午10:24:33 
 * @version V1.0
 */

public class SortDecorator extends Decorator{
	
	/**
	 *@Description:构造方法。获取被修饰类。当然被修饰过的类也可以做为修饰类。这就是为什么Decorator要去继承修饰类或接口。
	 **/
	public SortDecorator(ISchoolreport report){
		super(report);
	}
	
	/**
	 *@Description:修饰方示。
	 */
	public void sortDecorator(){
		System.out.println("排名低20");
	}
	/**
	 *@Description: 重写父类方法。 
	 */
	public void report(){
		this.sortDecorator();
		super.report();
	}
}

这个孩子说自己的班上排名20,那一样的肯定也不错了撒!

很好!

如果那天你想两个都装饰下,那也可以。

/** 
 * @ClassName: Client 
 * @Description:装饰模式客户端。
 * @author <helloyangzhi@foxmail.com>
 * @date 2012-12-15 上午10:13:28 
 * @version V1.0
 */

public class Client {
	
	
	public static void main(String[] args) {
		
		//未装饰
		ISchoolreport is = new FourthSchoolReport();
		is.report();
		
		//装饰一下
		is = new ScoreDecorator(is);
		is.report();
		
		is = new SortDecorator(is);
		is.report();
	}

}

这样两个都被你装饰上了。如果你还想修饰下,可以自忆再建个类就可以了!

抱歉!评论已关闭.