Java Learning notes-design pattern Vi. prototype mode (shallow clone and Deep clone)

Source: Internet
Author: User

That there's some good in the world, Mr Frodo. And it ' s worth fighting for.

Prototype mode (prototype), which specifies the kind of object created with the prototype instance, and creates a new object by copying the prototypes.

As for Object.clone (), we will not dwell on it here. The document may seem a little confusing, and the code is easier to understand:

Shallow clone:

1 Package Cn.happy.design_pattern._06prototype.shallowclone;2 3  Public Abstract classPrototype {4 5     PrivateString ID;6 7 String getId () {8         returnID;9     }Ten  One     voidsetId (String id) { A          This. ID =ID; -     } -      the      Public AbstractPrototype Mclone (); -}

1 Package Cn.happy.design_pattern._06prototype.shallowclone;2 3  Public classConcreteprototype extends Prototype implements cloneable{4 5     Privateobj obj;6      PublicConcreteprototype () {7 super ();8obj =NewObj ();9     }Ten  One     /* A * Getter and setter -      */ - OBJ getobj () { the         returnobj; -     } -  -  +     voidsetobj (obj obj) { -          This. obj =obj; +     } A  at  - @Override -      PublicPrototype Mclone () { -         //TODO auto-generated Method Stub -Prototype Prototype =NULL; -         Try { inPrototype = (prototype) This. Clone (); -}Catch(clonenotsupportedexception e) { to             //TODO auto-generated Catch block + e.printstacktrace (); -         } the         returnprototype; *     } $ Panax Notoginseng}

1 Package Cn.happy.design_pattern._06prototype.shallowclone;2 3  Public classOBJ {4 5     PrivateString name;6 7 String GetName () {8         returnname;9     }Ten  One     voidsetName (String name) { A          This. Name =name; -     } -      the}

1 Package Cn.happy.design_pattern._06prototype.shallowclone;2 3  Public classMmain {4 5      Public Static voidMain (string[] args) {6Concreteprototype C1 =NewConcreteprototype ();7Concreteprototype C2 =(Concreteprototype) C1.mclone ();8C1.setid ("prototypes");9C1.getobj (). SetName ("Zhang San");TenC2.setid ("Copy"); OneC2.getobj (). SetName ("John Doe"); A         /* - * Shallow cloning: - * ID is a value type variable, bit-wise copy, generating a new variable address; obj is a reference variable, copy only references, copy and prototype share the same variable address; the * Output: [email protected], prototype, John Doe - * [email protected], copy, John Doe -          */ -System. out. println (c1+","+c1.getid () +","+c1.getobj (). GetName ()); +System. out. println (c2+","+c2.getid () +","+c2.getobj (). GetName ()); -  +     } A  at}

There are two special types of value-type variables that need to be mentioned here, strings (in fact, strings are char arrays) and arrays. Although they are reference types, they can be shallow clones. Everyone will know when they try.

Deep clones:

The Prototype class does not change.

1 Package Cn.happy.design_pattern._06prototype.deepclone;2 3  Public classConcreteprototype extends Prototype implements cloneable{4 5     Privateobj obj;6      PublicConcreteprototype () {7 super ();8obj =NewObj ();9     }Ten     //Adding a new construction method One     Privateconcreteprototype (obj obj) { A super (); -          This. obj =(OBJ) Obj.mclone (); -     } the  -  -     /* - * Getter and setter +      */ - OBJ getobj () { +         returnobj; A     } at  -  -     voidsetobj (obj obj) { -          This. obj =obj; -     } -  in  - @Override to      PublicPrototype Mclone () { +         //TODO auto-generated Method Stub -Prototype Prototype =NULL; the         //call the new constructor method *Prototype =NewConcreteprototype (Getobj ()); $ Prototype.setid (GetId ());Panax Notoginseng         returnprototype; -     } the  +}

1 Package Cn.happy.design_pattern._06prototype.deepclone;2 3  Public classOBJ implements cloneable{4Omitting attributes, this may require a deeper clone.5      PublicObject Mclone () {6obj obj =NULL;7         Try {8obj = (obj) This. Clone ();9}Catch(clonenotsupportedexception e) {Ten             //TODO auto-generated Catch block One e.printstacktrace (); A         } -         returnobj; -     } the}

Test class:

1 Package Cn.happy.design_pattern._06prototype.deepclone;2 3  Public classMmain {4 5      Public Static voidMain (string[] args) {6Concreteprototype C1 =NewConcreteprototype ();7Concreteprototype C2 =(Concreteprototype) C1.mclone ();8         /*9 * Deep Clone: C1 is different from Obj's address in C2Ten * output: [email protected],null,[email protected] One * [email protected],null,[email protected] A          */ -System. out. println (c1+","+c1.getid () +","+c1.getobj ()); -System. out. println (c2+","+c2.getid () +","+c2.getobj ()); the  -     } -  -}

Java Learning notes-design pattern Vi. prototype mode (shallow clone and Deep clone)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.