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(); } }