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