JAVA装饰模式,封装父类,提供转换接口_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > JAVA装饰模式,封装父类,提供转换接口

JAVA装饰模式,封装父类,提供转换接口

 2012/2/28 18:01:30  numen06  程序员俱乐部  我要评论(0)
  • 摘要:packagecom.wesley.framework.decoration;importjava.util.Collection;importjava.util.List;publicinterfaceDecoration<Model,DecorextendsDecorator<Model>>{publicList<Decor>baseExchange(Collection<Model>models)
  • 标签:Java 模式 接口 装饰模式
package com.wesley.framework.decoration;

import java.util.Collection;
import java.util.List;

public interface Decoration<Model, Decor extends Decorator<Model>> {

	public List<Decor> baseExchange(Collection<Model> models);

	public Decor baseExchange(Model model);

}

package com.wesley.framework.decoration;

public interface Decorator<T> extends java.io.Serializable {

	public void setModel(T model);

	//public T getModel();
}

package com.wesley.framework.decoration;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@SuppressWarnings("serial")
public abstract class DecoratorModel<Model, Decor extends Decorator<Model>>
		implements Decorator<Model>, Decoration<Model, Decor> {

	protected Model model;

	public DecoratorModel() {
		super();
	}

	public DecoratorModel(Model model) {
		super();
		this.model = model;
	}

//	@Override
//	public Model getModel() {
//		return model;
//	}

	/* (non-Javadoc)
	 * @see com.wesley.framework.decoration.Decorator#setModel(java.lang.Object)
	 * 将Model注入到装饰器中
	 */
	@Override
	public void setModel(Model model) {
		this.model = model;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.wesley.framework.decoration.Decoration#baseExchange(java.lang.Object)
	 * 
	 * 反射必有参数构造函数,将实体包含在装饰器之中
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Decor baseExchange(Model model) {
		Decor decor = null;
		try {
			decor = (Decor) this.getClass().getConstructor(model.getClass())
					.newInstance(model);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		decor.setModel(model);
		return decor;
	}

	/* (non-Javadoc)
	 * @see com.wesley.framework.decoration.Decoration#baseExchange(java.util.Collection)
	 * 转换List等Collection接口数据
	 */
	@Override
	public List<Decor> baseExchange(Collection<Model> models) {
		List<Decor> decorList = new ArrayList<Decor>();
		for (Model model : models) {
			decorList.add(this.baseExchange(model));
		}
		return decorList;
	}
}

package com.meadville.entity.decoration;

import com.meadville.entity.Tlog;
import com.wesley.framework.decoration.DecoratorModel;

/**
 * @author it.zl 继承装饰父类的装饰类
 */
@SuppressWarnings("serial")
public class LogDec extends DecoratorModel<Tlog, LogDec> {
	public LogDec() {
		super();
	}

	/**
	 * @param log
	 *            通过构造函数初始化装饰类
	 */
	public LogDec(Tlog log) {
		super(log);
	}

	/**
	 * @return 从被封装类中取出数据
	 */
	public Integer getId() {
		return super.model.getId();
	}
}
发表评论
用户名: 匿名