建造者模式

将一个复杂对象分解成多个相对简单的部分,然后根据不同需要分别创建它们,最后构建成该复杂对象。

JDK 中 java.lang.StringBuilder 和 java.lang.StringBuffer 两个对字符串操作的类使用了建造者模式。类中的 append、delete、replace、insert、deleteCharAt、appendCodePoint 方法,通过传入字符或字符串相关的参数,return this 构造对象。

public StringBuilder append(String str) {    super.append(str);    return this;}
public StringBuilder appendCodePoint(int codePoint) {    super.appendCodePoint(codePoint);    return this;}
public StringBuilder delete(int start, int end) {    super.delete(start, end);    return this;}
public StringBuilder deleteCharAt(int index) {    super.deleteCharAt(index);    return this;}
public StringBuilder replace(int start, int end, String str) {    super.replace(start, end, str);    return this;}
public StringBuilder insert(int index, char[] str, int offset, int len){    super.insert(index, str, offset, len);    return this;}


原型模式

将一个对象作为原型,通过对其进行复制而克隆出多个和原型类似的新对象。在 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);}

 

图片


更多相关文章

  1. 构造方法的参数太多,如何解决?
  2. 面试官:为什么静态方法不能调用非静态方法和变量?
  3. ConcurrentHashMap之size()方法
  4. 为什么不推荐使用finalize方法,来看看对垃圾回收有什么影响吧

随机推荐

  1. 如何让父母与孩子一起成长?
  2. link标签和script标签跑到body下面,网页顶
  3. HTML高级标签之窗口分帧(前台界面演示)
  4. easyui-tree根据叶子节点获取父节点值(N
  5. HTML中的attribute和property的区别
  6. 夜灵的Html笔记Day04——CSS3basic
  7. 网页局部打印
  8. Html5的datetime-local控件
  9. 缓存js / css资源以使我的项目脱机工作
  10. 使用html5中的base64数据播放音频。