Java prototype mode (prototype)

Source: Internet
Author: User
To understand the prototype mode, you must first understand the shortest replication and deep replication in Java. In some cases, replication is also called cloning. Java provides these two cloning methods.

Shortest: all the variables of the cloned object contain the same value as the original object, and all its references to other objects still point to the original object. In other words, the shortest clone only clones the objects to be considered, rather than the objects referenced by it.

Deep clone: all the variables of the cloned object contain the same value as the original object, but all its references to other objects are no longer original, this is a new object that has been copied. In other words, deep replication copies all referenced objects of the objects to be copied. This is called indirect replication. The source code of deep replication is as follows: Java code

  1. Public object deepclone () throws ioexception, optionaldataexception, classnotfoundexception
  2. {
  3. // Write to stream
  4. Bytearrayoutputstream BO = new bytearrayoutputstream ();
  5. Objectoutputstream oo = new objectoutputstream (BO );
  6. OO. writeobject (this); // read from stream bytearrayinputstream Bi = new bytearrayinputstream (BO. tobytearray ());
  7. Objectinputstream OI = new objectinputstream (BI );
  8. Return (OI. readobject ());
  9. }
Public object deepclone () throws ioexception, optionaldataexception, classnotfoundexception {// write to stream bytearrayoutputstream BO = new topology (); objectoutputstream oo = new objectoutputstream (BO); oo. writeobject (this); // read from stream bytearrayinputstream Bi = new bytearrayinputstream (BO. tobytearray (); objectinputstream OI = new objectinputstream (BI); Return (OI. readobject ());}

The build model of the Java language directly supports the prototype model. All classes to be cloned must implement a cloneable interface. All classes have a clone () method (provided by the object ). Cloning meets the following conditions: X. Clone () exists for any object X ()! = X, in other words, the cloned object is not the original object; X. clone (). getclass () = x. getclass (). In other words, the cloned object is of the same type as the original object. X. Clone (). Equals (X) is set up. Java code

  1. Public class sheep implements cloneable
  2. {
  3. Private string name = "Dolly ";
  4. Public object clone () throws clonenotsupportedexception
  5. {
  6. Return super. Clone ();
  7. }
  8. }
Public class sheep implements cloneable {private string name = "Dolly"; public object clone () throws clonenotsupportedexception {return Super. Clone ();}}

Prototype mode definition: Use a prototype instance to specify the type of the object to be created, and copy the prototype to create a new object.

The prototype mode allows an object to create another custom object without any details. The working principle is: pass a prototype object to the object to be created, the objects to be created are created by requesting the prototype objects to copy themselves.

How to use it? Because the clone () method is provided in Java to clone objects, prototype implementation becomes very simple.

Using a spoon as an example: Java code

  1. Public abstract class abstractspoon implements cloneable
  2. {
  3. String spoonname;
  4. Public void setspoonname (string spoonname) {This. spoonname = spoonname ;}
  5. Public String getspoonname () {return this. spoonname ;}
  6. Public object clone ()
  7. {
  8. Object object = NULL;
  9. Try {
  10. Object = super. Clone ();
  11. } Catch (clonenotsupportedexception exception ){
  12. System. Err. println ("abstractspoon is not cloneable ");
  13. }
  14. Return object;
  15. }
  16. }
  17. There is a specific implementation (concreteprototype ):
  18. Public class soupspoon extends actspoon
  19. {
  20. Public soupspoon ()
  21. {
  22. Setspoonname ("soup spoon ");
  23. }
  24. }
Public abstract class implements actspoon implements cloneable {string spoonname; Public void setspoonname (string spoonname) {This. spoonname = spoonname;} Public String getspoonname () {return this. spoonname;} public object clone () {object = NULL; try {object = super. clone ();} catch (clonenotsupportedexception exception) {system. err. println ("abstractspoon is not cloneable");} return object;} has a specific implementation (concreteprototype): Public class soupspoon extends actspoon {public soupspoon () {setspoonname ("soup spoon ");}}

It is easy to call the prototype mode:

Abstractspoon spoon = new soupspoon ();
Abstractspoon spoon2 = spoon. Clone ();
Of course, you can also create a abstractspoon instance in the factory mode.

In Java, the prototype mode becomes the use of the clone () method. The pure object-oriented feature of Java makes it natural to use the design mode in Java, the two are almost integrated. This is reflected in many modes, such as the interator traversal mode.

 

From: http://sakyone.iteye.com/blog/484099

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.