[size=x-small]http://numen06.iteye.com/blog/1428067
上一篇文章提到了装饰类的父类,对于一些基本功能的封装。
在实际运用过程中,带的类型有两个太复杂, 不利于装饰类的扩展。
所以功能和装饰类区分,以便更好的扩展。
[/size]
上一篇文章提到了装饰类的父类,对于一些基本功能的封装。
在实际运用过程中,带的类型有两个太复杂, 不利于装饰类的扩展。
所以功能和装饰类区分,以便更好的扩展。
[/size]
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 com.wesley.framework.commen.GenericsUtils;
@SuppressWarnings("serial")
public abstract class DecoratorModel<Model> implements Decorator<Model> {
protected 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);
}
@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;
// 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;
// }
}
package com.wesley.framework.decoration;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class DecoratorHelper<Model, Decor extends Decorator<Model>> implements
Decoration<Model, Decor> {
private Class<Decor> clazz;
public DecoratorHelper(Class<Decor> dec) {
super();
this.clazz = dec;
}
@SuppressWarnings("unchecked")
public DecoratorHelper(Decor dec) {
super();
this.clazz = (Class<Decor>) dec.getClass();
}
/*
* (non-Javadoc)
*
* @see
* com.wesley.framework.decoration.Decoration#baseExchange(java.lang.Object)
*
* 反射必有参数构造函数,将实体包含在装饰器之中
*/
@Override
public Decor baseExchange(Model model) {
Decor decor = null;
try {
decor = clazz.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;
}
}