JAVA Design Pattern Prototype pattern

Source: Internet
Author: User

Use

prototype mode (Prototype) uses a prototype instance to specify the kind of object to create, and creates a new object by copying the prototypes.

Prototype mode is a Create pattern

structure diagram-prototype pattern structure diagram Prototype: Declares an interface to the clone itself.
Concreteprototype: Implements the specific operation of the clone itself.
Client: Call Prototype to clone itself, creating a new object.

Application Scenarios

When the class to instantiate is specified at run time, for example, by dynamic loading.
To avoid creating a factory class hierarchy that is parallel to the product class hierarchy.
When an instance of a class can have only one of several different state combinations. Building the corresponding number of prototypes and cloning them may be more convenient than manually instantiating the class each time you use the appropriate state.


shallow copy and deep copy

Shallow copy means that the object referenced by the field is not copied when the field value of the object is copied.
For example, if an object has a field that points to a string, and we make a shallow copy of the object, then two objects will reference the same string.
A deep copy is when a class has resources, and when the object of that class has a replication process, the resource is redistributed, and the process is a deep copy.

Template method

The prototype pattern is mainly used for object replication, and its core is the prototype class prototype in the class diagram. The prototype class needs to have the following two conditions:

    1. Implements the Cloneable interface. In the Java language, there is a cloneable interface, which only works by notifying the virtual machine at runtime that it can safely use the Clone method on classes that implement this interface. In a Java virtual machine, only classes that implement this interface can be copied, or clonenotsupportedexception exceptions will be thrown at run time.
    2. Override the Clone method in the object class. In Java, the parent class of all classes is the object class, and the object class has a Clone method that returns a copy of the object, but its scope is protected type, and the generic class cannot be called, so The prototype class needs to modify the scope of the Clone method to the public type.
Prototype mode is a relatively simple mode, it is very easy to understand, implement an interface, rewrite a method to complete the prototype mode. In practical applications, prototype patterns rarely appear alone. Often mixed with other patterns, his prototype class prototype also uses abstract classes instead.

Code

classPrototypeImplementscloneable {
PublicPrototype Clone () {
Prototype Prototype =NULL;
Try{
Prototype = (prototype)Super. Clone ();
}Catch(Clonenotsupportedexception e) {
E.printstacktrace ();
}
returnPrototype
}
}

classConcreteprototypeextendsPrototype {
PublicvoidShow () {
SYSTEM.OUT.PRINTLN ("Prototype Pattern implementation class");
}
}

PublicclassPrototypepattern {
PublicStaticvoidMain (string[] args) {
Concreteprototype CP =NewConcreteprototype ();
for(inti=0; i< 10; i++) {
Concreteprototype CLONECP = (concreteprototype) cp.clone ();
Clonecp.show ();
}
}
}


Reference

"Big Talk design mode"

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

JAVA Design Pattern Prototype pattern

Related Article

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.