Design mode (quad) prototype mode (PROTOTYPE)-Create type

Source: Internet
Author: User

Prototype Mode prototype

Prototype mode is also a mode of creation, as the name implies, is to create a new object identical to the original. For example, the daily development, commonly used in the replication function, the user creates a new financial voucher, the future will have similar credentials, but the amount needs to be modified, the other content is the same. At this point, the user does not need to re-enter the contents of the credential one by one into the interface, just to perform the copy function, you can generate a new identical voucher, and then the new voucher only modify the amount, then use the prototype mode.

Description: Java An object assignment is an address pass, and modifying an assigned instance modifies the property value of the original instance, and we need to modify the property value of the original instance without modifying it after copying it.

Prototype mode


method: Implement the Cloneable class in Java

Implements Cloneable

Public Object Clone () {Object obj = Null;try{obj = Super.clone ();} catch (Clonenotsupportexception ex) {System.out.println (ex);} return OJB;}

Client calls:

Product p = new product ();p. Setnum (111);p. Setprice (+);p. Setrebate (20); Product P1 = (product) p.clone ();p 1.setNum (200);

What we get is that the number of modified P1 and P is different.




Design mode (quad) prototype mode (PROTOTYPE)-Create type

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.