简单_原型模式(Prototype)
原型模式说白了也就是克隆自身,为了解决某些复杂对象的创建工作而生的。
网上许多关于原型模式的文章东西倒是说了很多而且还扯到了工厂模式等一些其他的模式,
我个人认为那样真是啰嗦的要死不说还把问题搞负责了。
克隆分为浅克隆和深克隆,浅克隆紧紧克隆对象的基本数据类型的值,深克隆会连引用类型也克隆。JAVA中凡是实现了java.lang.Cloneable接口的类都具有克隆功能,而这样的克隆是浅克隆的,深克隆的原理是:通过把克隆对象序列化到内存然后再反序列化读出。
/** * Returns a shallow copy of this <tt>HashMap</tt> instance: the keys and * values themselves are not cloned. * * @return a shallow copy of this map. */ public Object clone() { HashMap<K,V> result = null;try { result = (HashMap<K,V>)super.clone();} catch (CloneNotSupportedException e) { // assert false;} result.table = new Entry[table.length]; result.entrySet = null; result.modCount = 0; result.size = 0; result.init(); result.putAllForCreate(this); return result; }