首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

JAVA装饰模式,打包父类,提供转换接口(三)

2012-08-29 
JAVA装饰模式,封装父类,提供转换接口(三)http://numen06.iteye.com/blog/1428067 http://numen06.iteye.co

JAVA装饰模式,封装父类,提供转换接口(三)
http://numen06.iteye.com/blog/1428067
http://numen06.iteye.com/blog/1439763
上一篇文章已经将封装转换类,独立出来并作为一个类来进行。

在实际运用过程的中会遇到初始化的时候先后问题,所以将装换借口直接做成static只是作为工具类运用。

package com.wesley.framework.decoration;import java.util.ArrayList;import java.util.Collection;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Set;import org.apache.commons.beanutils.BeanUtils;import org.apache.commons.collections.MapUtils;import com.wesley.framework.commen.ArrayUtil;import com.wesley.framework.commen.ObjectUtil;import com.wesley.framework.commen.reflect.ClassUtil;import com.wesley.framework.commen.reflect.ReflectionUtils;public class DecoratorFactory {@SuppressWarnings({ "unchecked"})public synchronized static <Model, Decor extends Decorator<Model>, SuperDec extends Decorator<?>> Decor exchange(SuperDec superDec, Class<Decor> clazz, String propertyName,String... params) {Decor decor = null;try {decor = ClassUtil.newInstance(clazz);Model model = (Model) ReflectionUtils.invokeGetterMethod(superDec.getModel(), propertyName);if (ObjectUtil.isEmpty(model)) {ReflectionUtils.invokeSetterMethod(superDec, propertyName,decor);return decor;}Map<String, String> map = ArrayUtil.toMap(params);if (!MapUtils.isEmpty(map))BeanUtils.populate(decor, map);decor.setModel(model);} catch (Exception e) {e.printStackTrace();}return decor;}public synchronized static <Model, Decor extends Decorator<Model>> Decor exchange(Class<Decor> clazz, Model model, String... params) {Decor decor = null;try {if (ObjectUtil.isEmpty(model))return null;decor = ClassUtil.newInstance(clazz);Map<String, String> map = ArrayUtil.toMap(params);if (!MapUtils.isEmpty(map))BeanUtils.populate(decor, map);decor.setModel(model);} catch (Exception e) {e.printStackTrace();}return decor;}@SuppressWarnings("unchecked")public static <Model, Decor extends Decorator<Model>> List<Decor> exchange(Decor dec, Collection<Model> models, String... params) {List<Decor> decorList = new ArrayList<Decor>();try {for (Model model : models) {decorList.add((Decor) DecoratorFactory.exchange(dec.getClass(),model, params));}} catch (Exception e) {e.printStackTrace();}return decorList;}public static <Model, Decor extends Decorator<Model>> List<Decor> exchange(Class<Decor> clazz, Collection<Model> models, String... params) {List<Decor> decorList = new ArrayList<Decor>();try {for (Model model : models) {decorList.add(DecoratorFactory.exchange(clazz, model, params));}} catch (Exception e) {e.printStackTrace();}return decorList;}public static <Model, Decor extends Decorator<Model>> Set<Model> exchange(Collection<Decor> decs, String... params) {Set<Model> decorList = new HashSet<Model>();try {for (Decor dec : decs) {Model model = dec.getModel();Map<String, String> map = ArrayUtil.toMap(params);if (!MapUtils.isEmpty(map))BeanUtils.populate(model, map);decorList.add(model);}} catch (Exception e) {e.printStackTrace();}return decorList;}}

package com.wesley.framework.decoration;import org.apache.struts2.json.annotations.JSON;import com.wesley.framework.commen.reflect.GenericsUtils;@SuppressWarnings("serial")public abstract class DecoratorModel<Model> implements Decorator<Model> {private Model model;// protected DecoratorHelper<Model, ? extends Decorator<Model>> helper;/** * 装饰器构造函数,如果没有自动创建一个实体 */@SuppressWarnings("unchecked")public DecoratorModel() {super();try {Class<Model> cls = GenericsUtils.getSuperClassGenricType(this.getClass(), 0);this.setModel(cls.newInstance());} catch (InstantiationException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}}/** * @param model *            将实体封装进入装饰器 */public DecoratorModel(Model model) {super();this.setModel(model);}@JSON(serialize = false)@Overridepublic final Model getModel() {return this.model;}/* * (non-Javadoc) *  * @see com.wesley.framework.decoration.Decorator#setModel(java.lang.Object) * 装饰器接口,将Model注入到装饰器中 */@Overridepublic void setModel(Model model) {this.model = model;// helper = new DecoratorHelper<Model,Decorator<Model>>(// (Class<Decorator<Model>>) this.getClass());}// /*// * (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;// }}

热点排行