原型模式是将一个对象作为原型,通过对其进行复制而克隆出多个和原型类似的新对象。
在 JDK 中所有类的默认父类 java.lang.Object 中的 clone 方法,就实现快速地浅拷贝一个对象,当然有个前提条件,就是被克隆的对象的类需要实现 Cloneable 接口,否则会抛出异常 CloneNotSupportedException。
/**
* Creates and returns a copy of this object. The precise meaning
* of "copy" may depend on the class of the object.
*/
protected native Object clone() throws CloneNotSupportedException;
Java 默认的 clone 方法是浅拷贝,那如何实现深拷贝呢?
- 实现 Cloneable 接口,递归 clone 引用对象或 new 新对象(类的属性字段未实现 Cloneable 接口)
- 借助序列化完成深拷贝,如实现 JDK java.io.Serializable 接口、json格式序列化、xml格式序列化等
在我们平时开发中,也会使用 Spring 中 org.springframework.beans.BeanUtils 的 copyProperties 方法复制一个对象的属性到另一个对象
public static void copyProperties(Object source, Object target) throws BeansException {
copyProperties(source, target, null, (String[]) null);
}
apache commons-beanutils 包中 org.apache.commons.beanutils.BeanUtils 工具类中有 cloneBean 方法无需实现 Cloneable 接口的浅拷贝,也有 copyProperties 和 copyPropertie 方法复制对象属性和指定属性进行复制。
/**
* <p>Clone a bean based on the available property getters and setters,
* even if the bean class itself does not implement Cloneable.</p>
*
* <p>
* <strong>Note:</strong> this method creates a <strong>shallow</strong> clone.
* In other words, any objects referred to by the bean are shared with the clone
* rather than being cloned in turn.
* </p>
*/
public Object cloneBean(Object bean)
throws IllegalAccessException, InstantiationException,
InvocationTargetException, NoSuchMethodException {
if (log.isDebugEnabled()) {
log.debug("Cloning bean: " + bean.getClass().getName());
}
Object newBean = null;
if (bean instanceof DynaBean) {
newBean = ((DynaBean) bean).getDynaClass().newInstance();
} else {
newBean = bean.getClass().newInstance();
}
getPropertyUtils().copyProperties(newBean, bean);
return (newBean);
}
【Java学习资源】整理推荐
【Java面试题与答案】整理推荐