Design mode: Prototype mode (PROTOTYPE)

Source: Internet
Author: User

Prototype mode: Use the prototype instance to specify the kind of object to create and create new objects by copying the prototypes.

? For example:
1 prototype (actually implements the Cloneable interface, which in turn overrides the Clone () method)

 Public  class ProtoType implements cloneable{    PrivateString name;@Override     PublicProtoTypeClone()    {Try{return(ProtoType)Super. Clone (); }Catch(Clonenotsupportedexception e)        {E.printstacktrace (); }return NULL; } PublicStringGetName()    {returnName } Public void SetName(String name) { This. name = name; }}

2 Test Code

publicclass MainTest{    publicstaticvoidmain(String[] args)    {        new ProtoType();        pt1.setName("protoType_1");        ProtoType pt2 = pt1.clone();        System.out.println(pt1 == pt2);        System.out.println(pt1.getClass() == pt2.getClass());        pt2.setName("protoType_2");        System.out.println(pt1.getName()+" "+pt2.getName());    }}

Output:

falsetrueprotoType_1 protoType_2

Cloning is also divided into shallow clones and deep clones, details here will not repeat. In "Effective Java", the author put forward the "careful coverage of clone", the paper puts forward a lot of cloneable problems, it is recommended to use the copy constructor method to replace the cloneable.

Pros and cons
Advantages:

    1. If you are creating new objects that are more complex, you can use prototype mode to simplify the creation of objects and also to improve efficiency.
    2. You can use deep clones to persist the state of an object.
    3. Prototype mode provides a simplified creation structure.

Disadvantages:

    1. Complex code may be needed when implementing deep clones.
    2. Each class needs to be equipped with a cloning method, and this cloning method needs to take a holistic view of the function of the class, which is not very difficult for the new class, but it is not always easy to modify the existing class, it must change its source code and violate the "open and close principle".

Applicable Scenarios
1. If the cost of creating a new object is large, we can use the existing object for replication to obtain.
2. If the system wants to save the state of an object, and the state of the object changes very little, or the object itself occupies little memory, you can also use the prototype mode with the memo mode to apply. Conversely, if the state of an object varies greatly, or if the object occupies a large amount of memory, then the state pattern is better than the prototype pattern.
3. To avoid using hierarchical factory classes to create hierarchical objects, and instances of classes that have only one or few combined states, it may be more convenient to copy a prototype object to get a new instance than to create a new instance using a constructor.

Prototype patterns in the JDK
Java.lang.object#clone ()
Java.lang.Cloneable

Resources:
1. "23 Design Patterns"
2. "Effective Java (Second Edition)" Joshua Bloch.
3. Design patterns in the "digital JDK"

Design mode: Prototype mode (PROTOTYPE)

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.