模式代码 java中aes_建造者模式和原型模式在开源代码中的应用
建造者模式
将一个复杂对象分解成多个相对简单的部分,然后根据不同需要分别创建它们,最后构建成该复杂对象。
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 方法复制对象属性和指定属性进行复制。
/** *
Clone a bean based on the available property getters and setters,
* even if the bean class itself does not implement Cloneable. * * * Note: this method creates a shallow clone. * In other words, any objects referred to by the bean are shared with the clone * rather than being cloned in turn. * */ 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);}