Design mode (9)-----prototype mode

Source: Internet
Author: User

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

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.