Prototype mode (PROTOTYPE) definition
Use the prototype instance to specify the kind of object to create and create a new object by copying the prototypes.
UML Structure diagram
Shallow copy
ProtoType
PackageCom.csdhsm.pattemdesign.prototype;Importjava.util.ArrayList;Importjava.util.List;/*** @Title: Prototype.java * @Description: Light copy *@author: Han * @date: June 21, 2016 PM 9:09:28*/ Public classPrototypeImplementscloneable {PrivateString name; Privatelist<string> list =NewArraylist<>(); PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public voidAdd (String str) { This. List.add (str); } PublicList<string>getList () {returnlist; } Public voidSetlist (list<string>list) { This. List =list; } //The Clone method returns an object@OverrideprotectedObject Clone () {Try { return Super. Clone (); } Catch(clonenotsupportedexception e) {e.printstacktrace (); return NULL; } }}
Client
PackageCom.csdhsm.pattemdesign.prototype; Public classSolution { Public Static voidMain (string[] args) {Prototype Pro=NewPrototype (); Pro.setname ("Original Object"); Pro.add ("Original Object1"); Prototype Pro1=(Prototype) Pro.clone (); Pro1.setname ("Changed Object1"); Pro1.add ("Original Object2"); System.out.println ("Original object:" +pro.getname ()); System.out.println ("Cloned object:" +pro1.getname ()); System.out.println ("Original object:" +pro.getlist ()); System.out.println ("Cloned object:" +pro1.getlist ()); }}
Results
Problem analysis
In the basic data type (Int,double,float ...) And a String object, this kind of replication is not a problem, because shallow copy only copies the value, if it is a reference type variable (for example, List,person ...) , only the reference is copied, that is, in memory, before and after the copy in memory is pointing to the same instance, so in the last example, for the list object, the generated object after copying the data, will cause the original object changes, this is called deep copy and shallow copy of the difference.
Deep copy
ProtoType
PackageCom.csdhsm.pattemdesign.prototype;Importjava.util.ArrayList;/*** @Title: Prototype.java * @Description: Deep copy *@author: Han * @date: June 21, 2016 PM 9:09:28*/ Public classPrototypeImplementscloneable {PrivateString name; Privatearraylist<string> list =NewArraylist<>(); PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public voidAdd (String str) { This. List.add (str); } PublicArraylist<string>getList () {returnlist; } Public voidSetlist (arraylist<string>list) { This. List =list; } //The Clone method returns an object@SuppressWarnings ("Unchecked") @OverrideprotectedObject Clone () {Prototype Pro2=NULL; Try{Pro2= (Prototype)Super. Clone (); //The deep copy focuses on this code, re-emerging as an object, assigning a list to the new objectPro2.setlist ((arraylist<string>) This. List.clone ()); } Catch(clonenotsupportedexception e) {e.printstacktrace (); } returnPro2; }}
The client code does not change
Results
OK, deep copy success!
Design mode (9)-----prototype mode