Design Mode [prototype mode]

Source: Internet
Author: User

Use a prototype instance to point to the type of the created object and copy the prototype to create a new object. The prototype mode allows one object to create another customizable object without any details about how to create it. Working principle: by passing a prototype object to the object to be created, the object to be created is created by requesting the prototype object to copy themselves. It is mainly faced with the creation of "some objects with complex structures". due to changes in requirements, these objects often face drastic changes, however, they have stable and consistent interfaces.

The biggest feature of the prototype mode is to clone an existing object. There are two types of clone results.

1. Light replication,

2. Deep replication.

Here we need to discuss the principles of shortest replication and deep replication, which may make it easier for you to understand the usage of this prototype. We all know that the creation mode is generally used to create a new object, and then we use this object to complete some object operations, in the prototype mode, we can quickly create an object without providing special new () operations. This is an effective method, quickly create a new object.

[Shallow copy]All variables of the Copied object contain the same value as the original object, and all references to other objects still point to the original object. In other words, the shortest copy only copies the objects to be considered, rather than the objects referenced by the shortest copy.

[Deep replication]All variables of the Copied object contain the same value as the original object, except those variables that reference other objects. Variables that reference other objects will point to new objects that have been copied, instead of the original referenced objects. In other words, deep replication copies all the objects referenced by the objects to be copied.

ForLight ReplicationAndDeep ReplicationIn short:

Shallow copy:Copy the value of a member variable of the value type. For a member variable of the reference type, only the reference is copied and the referenced object is not copied.
Deep replication:Copy the value of a member variable of the value type, and copy the referenced object of the referenced type.

Main application scenarios:

1. If our object type is not determined at the beginning, but determined at runtime, it is easier to clone a New Type through this type of object.

2. Sometimes we may need a copy of an object in a certain state in the actual project. This premise is very important. How can we understand this. For example, sometimes we need to compare the status of an object after processing and whether the status before processing has changed. Maybe we need to determine whether the status has changed before processing a specific segment, in this case, we need to clone the copy of the state of the object before executing the processing, and then compare the status after execution, such applications often appear in projects.

3. When we are dealing with some objects relatively simple, and the differences between objects are very small, it may be only fixed when several attributes are different, we may be more appropriate to use the prototype mode.

Demo case

Public class prototype implements cloneable, serializable {Private Static final long serialversionuid = 10l; private string strname; private refobject; Public String getstrname () {return strname ;} public void setstrname (string strname) {This. strname = strname;} public refobject getrefobject () {return refobject;} public void setrefobject (refobject) {This. refobject = refobject;}/*** shallow copy * Copy the value of a member variable of the value type. For a member variable of the reference type, only the reference is copied and the referenced object is not copied. * // @ Overrideprotected object clone () throws clonenotsupportedexception {prototype = (prototype) super. clone (); Return prototype;}/** deep copy * copies the values of member variables of the value type, and copies the referenced objects of the referenced type. * @ Return * @ throws ioexception * @ throws classnotfoundexception */protected object deepclone () throws ioexception, writable {describo = new writable (); objectoutputstream oo = new objectoutputstream (BO ); oo. writeobject (this); bytearrayinputstream Bi = new bytearrayinputstream (BO. tobytearray (); objectinputstream OI = new objectinputstream (BI); return Oi. readobject ();}/*** class internal reference object, implement serializable interface * @ author mahc **/public class refobject implements serializable {Private Static final long serialversionuid = 101l ;}}

Prototype test cases.

Public class testprototype {public static void main (string [] ARGs) throws clonenotsupportedexception, ioexception, classnotfoundexception {prototype = new prototype (); refobject = prototype. new refobject (); prototype. setrefobject (refobject); prototype. setstrname ("Hello world! "); System. out. println ("using the shortest method to create objects"); prototype pt01 = (prototype) prototype. clone (); system. out. println ("==========================="); system. out. println ("compare the STR values of prototype and pt01:"); system. out. println (prototype. getstrname (); system. out. println (pt01.getstrname (); system. out. println ("/********************/"); system. out. println ("after modifying the str value in the pt1 object, compare the STR values of Pt and pt1:"); pt01.setstrname ("Hello, world"); system. out. println (prototype. getstrname (); system. out. println (pt01.getstrname (); system. out. println ("/********************/"); system. out. println ("compare the temp object values in prototype and pt01"); system. out. println (prototype. getrefobject (); system. out. println (pt01.getrefobject (); system. out. println ("==========================="); system. out. println ("using the deep COPY method to create objects"); pt01 = (prototype) prototype. deepclone (); system. out. println (prototype. getrefobject (); system. out. println (pt01.getrefobject ());}}

 

[For more information, see http://blog.csdn.net/mahoking]

 

References
1. http://luckykapok918.blog.163.com/blog/static/20586504320121145522355/

 

Design Mode [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.