Prototype mode of design pattern

Source: Internet
Author: User

The advantages and application scenarios of prototype mode

Creating an object using the prototype schema is much better performance than a direct new object, because the Clone method of the object class is a local method that directly operates in-memory binary streams, especially when copying large objects, where performance is significantly different.

Another benefit of using prototype mode is simplifying the creation of objects, making it as simple as copying and pasting as we edit the document.

Because of these advantages, you can consider using the prototype pattern when you need to create similar objects repeatedly. For example, to create objects in a loop, if the object creation process is complex or a lot of cycles, using prototype mode can not only simplify the creation process, but also can improve the overall performance of the system.

 PackageCom.test.pattern.prototype;classPrototypeImplementscloneable {@Override PublicObject Clone () {Prototype Prototype=NULL; Try{prototype= (Prototype)Super. Clone (); } Catch(Exception e) {e.printstacktrace (); }        returnprototype; }}classConcreteprototypeextendsPrototype { Public voidShow () {System.out.println ("Prototype Pattern implementation Class"); }}//Client Role Public classClient { Public Static voidMain (string[] args) {concreteprototype cp=NewConcreteprototype ();  for(inti = 0; I < 10; i++) {Concreteprototype CLONECP=(Concreteprototype) Cp.clone ();        Clonecp.show (); }    }}

Prototype mode of design pattern

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.