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)
@Override
public final Model getModel() {
return this.model;
}

/*
* (non-Javadoc)
*
* @see com.wesley.framework.decoration.Decorator#setModel(java.lang.Object)
* 装饰器接口,将Model注入到装饰器中
*/
@Override
public 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;
// }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值