1 definition: Specify the kind of objects created with the prototype instance, and create new objects by copying the prototypes. Allow one object to create another customizable object without knowing the details of how to create the 2 implementation method:by passing a prototype object to the object to be created, the object to be created will be created by requesting the prototype object to copy themselves. 3 usage: Many times, when creating objects, there is only a difference in some attribute values, and most are similar, the user of the class is how to create the object of the class, the internal structure of the class does not care about these things, but such an object is relatively complex initialization, And it takes a long time or resources, in which case we can consider using prototype mode. 4 prototype mode prototype-(Shallow clone): Shallow cloning refers to cloning an object, and the object's properties are just basic data types, only the object is cloned! eg
1 PackageCom.lovo.prototype.two;2 Public classPlayerImplementscloneable {3 4 Private intID;5 PrivateString name;6 PrivateString skill;7 8 Public intgetId () {9 returnID;Ten } One Public voidSetId (intID) { A This. ID =ID; - } - PublicString GetName () { the returnname; - } - Public voidsetName (String name) { - This. Name =name; + } - PublicString Getskill () { + returnskill; A } at Public voidSetskill (String skill) { - This. Skill =skill; - } - - - @Override in PublicString toString () { - return"Player [id=" + ID + ", name=" + name + ", skill=" + Skill + "]"; to } + - /** the * Shallow Clone * * @return $ * @throwsclonenotsupportedexceptionPanax Notoginseng */ - PublicPlayer Qianclone ()throwsclonenotsupportedexception{ the return(Player)Super. Clone (); + } A the + - $ $}
1 PackageCom.lovo.prototype.two;2 3 Public classTest {4 5 Public Static voidMain (string[] args)throwsclonenotsupportedexception {6Player p=NewPlayer ();7P.setid (1);8P.setname ("Xiao Ming");9P.setskill ("Slam Dunk");Ten OnePlayer p2=P.qianclone (); A - System.out.println (p); -System.out.println ("============="); the System.out.println (p2); -System.out.println (P==P2);//Fasle - - } + -}
5 prototype mode prototype-(Deep clone): Deep cloning refers to the target of the clone is also a reference type, reference type also has a reference type, and the reference type is cloned called deep cloning!
1 PackageCom.lovo.prototype.two;2 3 ImportJava.io.ByteArrayInputStream;4 ImportJava.io.ByteArrayOutputStream;5 Importjava.io.IOException;6 ImportJava.io.ObjectInputStream;7 ImportJava.io.ObjectOutputStream;8 Importjava.io.Serializable;9 Importjava.util.List;Ten One A - Public classPlayerImplementscloneable,serializable { - Private Static Final LongSerialversionuid = 1L; the Private intID; - PrivateString name; - PrivateString skill; - PrivateList<string>list; + - PublicList<string>getList () { + returnlist; A } at Public voidSetlist (list<string>list) { - This. List =list; - } - Public intgetId () { - returnID; - } in Public voidSetId (intID) { - This. ID =ID; to } + PublicString GetName () { - returnname; the } * Public voidsetName (String name) { $ This. Name =name;Panax Notoginseng } - PublicString Getskill () { the returnskill; + } A Public voidSetskill (String skill) { the This. Skill =skill; + } - $ @Override $ PublicString toString () { - return"Player [id=" + ID + ", name=" + name + ", skill=" +Skill -+ ", list=" + list + "]"; the } - Wuyi the /** - * Deep Cloning Wu * @return - * @throwsIOException About * @throwsclassnotfoundexception $ */ - PublicPlayer Deepclone ()throwsIOException, classnotfoundexception{ -Bytearrayoutputstream bos=NewBytearrayoutputstream (); -ObjectOutputStream oos=NewObjectOutputStream (BOS); AOos.writeobject ( This); + theBytearrayinputstream bis=NewBytearrayinputstream (Bos.tobytearray ()); -ObjectInputStream ois=NewObjectInputStream (bis); $Player p=(Player) ois.readobject (); the the the the - returnp; in the } the About the}
1 PackageCom.lovo.prototype.two;2 3 Importjava.io.IOException;4 Importjava.util.ArrayList;5 Importjava.util.List;6 7 Public classTest {8 9 Public Static voidMain (string[] args)throwsclonenotsupportedexception, ClassNotFoundException, IOException {TenPlayer p=NewPlayer (); OneP.setid (1); AP.setname ("Xiao Ming"); -P.setskill ("Slam Dunk"); - thePlayer p2=P.deepclone (); -List<string> list=NewArraylist<>(); -List.add ("Running"); -List.add ("Shooting"); + p2.setlist (list); - + A System.out.println (p); atSystem.out.println ("============="); - System.out.println (p2); - - } - -}
Prototype Mode prototype