Prototype mode: Create a new object through the prototype instance, no longer need to care about the type of the instance itself, as long as the method of cloning itself, you can use this method to obtain new objects, without having to go through new to create.
Prototype patterns are available in the following ways: Simple form (new object), Shallow clone, deep clone
The previous two methods are relatively simple, I directly with deep cloning to achieve, deep cloning is the flow of the way to achieve the redemption object cloning.
Monkey class:
Packageprototype;ImportJava.io.*;Importjava.util.Date; Public classMonkeyImplementscloneable,serializable {//Height Private intheight; //Weight Private intweight; //Birthday PrivateDate birthDate; //Golden Cudgel PrivateGoldringedstaff staff; PublicMonkey (Date birthDate, Goldringedstaff staff) { This. birthDate =birthDate; This. Staff =Staff ; } @OverrideprotectedObject Clone () {Monkey Monkey=NULL; Try{Monkey= (Monkey)Super. Clone (); } Catch(clonenotsupportedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } finally { returnMonkey; } } PublicObject Deepclone () {Try{ //write an object into the streamBytearrayoutputstream BOS =NewBytearrayoutputstream (); ObjectOutputStream Oos=NewObjectOutputStream (BOS); Oos.writeobject ( This); //read it from the stream.Bytearrayinputstream bis =NewBytearrayinputstream (Bos.tobytearray ()); ObjectInputStream Ois=NewObjectInputStream (bis); returnOis.readobject (); }Catch(Exception e) {return NULL; } } Public intgetheight () {returnheight; } Public voidSetHeight (intheight) { This. Height =height; } Public intgetweight () {returnweight; } Public voidSetweight (intweight) { This. Weight =weight; } PublicDate getbirthdate () {returnbirthDate; } Public voidsetbirthdate (Date birthDate) { This. birthDate =birthDate; } PublicGoldringedstaff Getstaff () {returnStaff ; } Public voidSetstaff (Goldringedstaff staff) { This. Staff =Staff ; }}
View Code
Golden Cudgel class:
Packageprototype;Importjava.io.Serializable; Public classGoldringedstaffImplementsSerializable {Private floatHeight = 100.0f; Private floatdiameter = 10.0f; /*** Growth behavior, each call length and radius increase by one times*/ Public voidgrow () { This. diameter *= 2; This. Height *= 2; } /*** Reduce the behavior by half the length and radius of each call*/ Public voidshrink () { This. diameter/= 2; This. Height/= 2; }}
View Code
Client class:
Packageprototype;Importjava.util.Date; Public classClient {PrivateMonkey Monkey =NewMonkey (NewDate (),NewGoldringedstaff ()); Public voidChange () {//Clone the Holy BuddhaMonkey Copymonkey =(Monkey) Monkey.deepclone (); System.out.println ("The birthday of the Venerable Master is:" +monkey.getbirthdate ()); System.out.println ("The birthday of the saint of clones is:" +monkey.getbirthdate ()); System.out.println ("The Holy Buddha and the clone of the Holy Sage is the same object" + (monkey = =Copymonkey)); System.out.println ("is the golden Cudgel held by the Holy Buddha the same object as the Golden Cudgel held by the cloned holy man?" "+ (monkey.getstaff () = =Copymonkey.getstaff ())); } Public Static voidMain (string[] args) {Client client=NewClient (); Client.change (); }}
View Code
Operation Result:
The birthday of the Holy Buddha is: Tue 22:27:35CST 2018 22:27:35 CST 2018falsefalse
Prototype patterns of Java design patterns