23 design modes (5): Prototype Mode

Source: Internet
Author: User

Definition:Use a prototype instance to specify the type of the object to be created, and copy the prototype to create a new object.

Type:Create Class Mode

Class diagram:

The prototype mode is mainly used for copying objects. Its core is the prototype of the prototype in the class diagram. The prototype class must meet the following two conditions:

  • Implement the cloneable interface. In Java, there is a cloneable interface, which serves only one function, that is, to notify the virtual machine to safely use the clone method on the class implementing this interface during execution. In a Java virtual machine, only the classes that implement this interface can be copied. Otherwise, a clonenotsupportedexception exception will be thrown during execution.
  • Override the clone method in the object class. In Java, the parent classes of all classes are object classes, and there is a clone method in the object class to return a copy of the object, but its scope of the protected type, the general class cannot be called, therefore, the prototype class must change the scope of the clone method to the public type.

The prototype mode is a simpler mode than the primitive mode. It is also easy to understand. To implement an interface, rewrite a method to complete the prototype mode. In practical applications, the prototype mode rarely appears separately. It is often mixed with other models, and its prototype is often replaced by abstract classes.

Implementation Code:

Class prototype implements cloneable {public prototype clone () {prototype = NULL; try {prototype = (prototype) super. clone ();} catch (clonenotsupportedexception e) {e. printstacktrace ();} return prototype;} class concreteprototype extends prototype {public void show () {system. out. println ("prototype implementation class") ;}} public class client {public static void main (string [] ARGs) {concreteprototype CP = new concreteprototype (); for (INT I = 0; I <10; I ++) {concreteprototype clonecp = (concreteprototype) CP. clone (); clonecp. show ();}}}

Advantages and application scenarios of the prototype model

Creating an object in the prototype mode is much better than simply creating a new object. Because the object class clone method is a local method, it directly operates the binary stream in the memory, especially when copying large objects, the performance difference is obvious.

Another advantage of the prototype mode is to simplify object creation, making it as simple as copying and pasting objects when editing documents.

Because of the above advantages, you can consider using the prototype mode when you need to create similar objects repeatedly. For example, you need to create an object in a loop. If the object creation process is more complex or has a large number of cycles, using the prototype mode not only simplifies the creation process, in addition, the overall performance of the system is greatly improved.

Precautions for prototype mode

  • Copying objects in prototype mode does not call the class constructor. Because the object replication is completed by calling the object class clone method, it directly copies data in the memory, so it does not call the class constructor. Not only does the code in the constructor not run, but even the allow permission is invalid for the prototype. Do you still remember the singleton mode? In Singleton mode, the singleton mode can be implemented only by setting the audit permission of the constructor to the private type. However, the clone method directly ignores the permissions of the constructor. Therefore, the singleton mode conflicts with the prototype mode. Pay special attention to this mode during use.
  • Deep copy and shallow copy. The clone method of the object class only copies the main data types in the object, and does not copy the array, container object, and referenced object. This is a shortest copy. For deep copy, the arrays, container objects, and reference objects in the prototype must be copied separately. For example:
public class Prototype implements Cloneable {private ArrayList list = new ArrayList();public Prototype clone(){Prototype prototype = null;try{prototype = (Prototype)super.clone();prototype.list = (ArrayList) this.list.clone();}catch(CloneNotSupportedException e){e.printStackTrace();}return prototype; }}

Because arraylist is not a basic type, the member variable list will not be copied. We need to implement deep copy by ourselves. Fortunately, most of the container classes provided by Java have implemented the cloneable interface. Therefore, it is not particularly difficult to implement deep copy.

PS: In the deep copy and shallow copy problems, there will be eight basic types and their encapsulation types in Java, and there will also be a string type. The rest are shortest copies.

23 design modes (5): 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.