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

[size=x-small]http://numen06.iteye.com/blog/1428067
上一篇文章提到了装饰类的父类,对于一些基本功能的封装。
在实际运用过程中,带的类型有两个太复杂, 不利于装饰类的扩展。
所以功能和装饰类区分,以便更好的扩展。
[/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;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值