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. There is a cloneable interface in Java, which has only one function. It notifies virtual machines at runtime that they can safely use the clone method in the class implementing this interface. In a Java virtual machine, only classes that implement this interface can be copied. Otherwise, a clonenotsupportedexception exception will be thrown during runtime.
  • Override the clone method in the object class. In Java, the parent class of all classes is an object class, and the object class has a clone method 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 modify the clone method scope to the public type.

The prototype mode is a simple mode and 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. prototype is also commonly used as an abstract class to replace it.

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 in terms of performance, because the object-class clone method is a local method that directly operates the binary stream in the memory, especially when copying large objects, the performance difference is very obvious.

Another advantage of using the prototype mode is to simplify object creation, making object creation as simple as copying and pasting during document editing.

Because of the above advantages, you can consider using the prototype mode when you need to repeatedly create similar objects. For example, you need to create an object in a circular body. If the object creation process is complex or has a large number of cycles, you can use the prototype mode to simplify the creation process and improve the overall performance of the system.

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 will the code in the constructor not be executed, but even the access permission is invalid for the prototype. Do you still remember the singleton mode? In Singleton mode, you only need to set the access permission of the constructor method to private. 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 when using it.
  • Deep copy and shallow copy. The clone method of the object class only copies the basic data types in the object, and does not copy the array, container object, and referenced object. This is a shortest copy. To implement deep copy, you must copy arrays, container objects, and reference objects in the prototype mode 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 on our own. Fortunately, most of the container classes provided by Java have implemented the cloneable interface. Therefore, implementing deep copy is not particularly difficult.

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.

From: http://blog.csdn.net/zhengzhb/article/details/7393528

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. There is a cloneable interface in Java, which has only one function. It notifies virtual machines at runtime that they can safely use the clone method in the class implementing this interface. In a Java virtual machine, only classes that implement this interface can be copied. Otherwise, a clonenotsupportedexception exception will be thrown during runtime.
  • Override the clone method in the object class. In Java, the parent class of all classes is an object class, and the object class has a clone method 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 modify the clone method scope to the public type.

The prototype mode is a simple mode and 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. prototype is also commonly used as an abstract class to replace it.

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 in terms of performance, because the object-class clone method is a local method that directly operates the binary stream in the memory, especially when copying large objects, the performance difference is very obvious.

Another advantage of using the prototype mode is to simplify object creation, making object creation as simple as copying and pasting during document editing.

Because of the above advantages, you can consider using the prototype mode when you need to repeatedly create similar objects. For example, you need to create an object in a circular body. If the object creation process is complex or has a large number of cycles, you can use the prototype mode to simplify the creation process and improve the overall performance of the system.

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 will the code in the constructor not be executed, but even the access permission is invalid for the prototype. Do you still remember the singleton mode? In Singleton mode, you only need to set the access permission of the constructor method to private. 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 when using it.
  • Deep copy and shallow copy. The clone method of the object class only copies the basic data types in the object, and does not copy the array, container object, and referenced object. This is a shortest copy. To implement deep copy, you must copy arrays, container objects, and reference objects in the prototype mode 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 on our own. Fortunately, most of the container classes provided by Java have implemented the cloneable interface. Therefore, implementing deep copy is not particularly difficult.

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.

From: http://blog.csdn.net/zhengzhb/article/details/7393528

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.