Detailed Java design pattern programming in the prototype model _java

Source: Internet
Author: User

Definition: Specifies the type of object to create with a prototype instance, and creates a new object by copying the prototypes.
Type: Creating class Mode
class Diagram:

The prototype model is mainly used for object replication, and its core is the prototype class prototype in the class diagram. The prototype class requires the following two conditions:
Implements the Cloneable interface. There is a cloneable interface in the Java language that has only one function, which is to notify the virtual machine at runtime that it is safe to use the Clone method on a class that implements this interface. In a Java virtual machine, only classes that implement this interface can be copied, or they will throw clonenotsupportedexception exceptions at run time.
Overrides the Clone method in the object class. In Java, the parent class of all classes is the object class, and there is a clone method in the object class that returns a copy of the object, but whose scope is protected type, and the generic class cannot be invoked, so The prototype class needs to modify the scope of the Clone method to the public type.
Prototype mode is a relatively simple pattern, and it is very easy to understand, implement an interface, rewrite a method that completes the prototype mode. In practical applications, prototype patterns rarely appear alone. Often mixed with other patterns, his prototype class prototype is also used as an alternative to abstract classes.
Implementation code:

Package Prototypepattern;
 
public class Prototypeclass implements cloneable{
  
 @Override
 protected Prototypeclass Clone () {
   
  Prototypeclass prototypeclass = null;
   
  try {
   Prototypeclass = (prototypeclass) super.clone ();
  } catch (Clonenotsupportedexception e) {
   //TODO Auto-generated Catch block
   e.printstacktrace ();
  }
   
  Return Prototypeclass
 }
}

Client:


Package Prototypepattern;
 
public class Client {public
 static void Main (string[] args) {
  Prototypeclass obj1 = new Prototypeclass ();
  Prototypeclass obj2 = Obj1.clone ();
   
  System.out.println (obj1);
  System.out.println (OBJ2);
 }

The advantages of the prototype model and the applicable scene
creating an object in prototype mode 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 replicating large objects.
Another benefit of using the prototype pattern is simplifying the creation of objects, making them as easy to copy and paste as we do when editing a document.
Because of the above advantages, you can consider using prototyping patterns when you need to create similar objects repeatedly. For example, the need to create objects in a loop, if the object creation process is more complex or a lot of cycles, the use of prototype mode can not only simplify the creation process, but also to improve the overall performance of the system.

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 
 } 
} 


Considerations for Prototyping Patterns:

    1. Constructors are not executed
    2. Shallow copy and deep copy
    3. Clone and Final Conflict

        since ArrayList is not a basic type, the member variable list is not copied and requires us to make deep copies of ourselves. Fortunately, most of the container classes provided by Java implement the Cloneable interface. So it's not particularly difficult to achieve deep copy.
PS: In the case of deep copy and shallow copy, the underlying types of 8 in Java and their encapsulation types, as well as the string type, can occur in deep copy. The rest are all shallow copies.

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.