Five prototype patterns for Java Design patterns (Prototype)

Source: Internet
Author: User
Tags shallow copy

Although the prototype pattern is a pattern of creation, it has nothing to do with the engineering model, and the idea of the pattern is to copy and clone an object as a prototype, and create a new object similar to the original object. This summary will be explained by the copy of the object. In Java, the Copy object is implemented through clone (), and a prototype class is created first:

[Java]View Plaincopy
    1. public class Prototype implements cloneable {
    2. Public Object Clone () throws Clonenotsupportedexception {
    3. Prototype proto = (Prototype) super.clone ();
    4. return proto;
    5. }
    6. }

Very simple, a prototype class, only need to implement the Cloneable interface, overwrite the Clone method, where the Clone method can be changed to any name, because the Cloneable interface is an empty interface, you can arbitrarily define the method name of the implementation class, such as Clonea or CLONEB, Because the focus here is Super.clone (), Super.clone () invokes the Clone () method of object, whereas in the object class, Clone () is native, and I'll do it in another article, The call to interpret native methods in Java is no longer a drill down here. Here, I'll take a look at the shallow copy and deep copy of the object, and first we need to understand the concept of deep, shallow copying of objects:

Shallow copy: After copying an object, the variables of the base data type are recreated, and the reference type, pointing to the original object.

Deep copy: After copying an object, both the base data type and the reference type are recreated. In short, deep replication is completely replicated, and shallow copying is not exhaustive.

Here, write an example of a depth copy:

[Java]View Plaincopy
  1. public class Prototype implements Cloneable, Serializable {
  2. Private static final long serialversionuid = 1L;
  3. private string string;
  4. Private Serializableobject obj;
  5. /* Shallow copy */
  6. Public Object Clone () throws Clonenotsupportedexception {
  7. Prototype proto = (Prototype) super.clone ();
  8. return proto;
  9. }
  10. /* Deep copy */
  11. Public Object Deepclone () throws IOException, ClassNotFoundException {
  12. /* writes the binary stream of the current object */
  13. Bytearrayoutputstream BOS = new Bytearrayoutputstream ();
  14. ObjectOutputStream oos = new ObjectOutputStream (BOS);
  15. Oos.writeobject (this);
  16. /* read out the new object generated by the binary stream */
  17. Bytearrayinputstream bis = new Bytearrayinputstream (Bos.tobytearray ());
  18. ObjectInputStream ois = new ObjectInputStream (bis);
  19. return Ois.readobject ();
  20. }
  21. Public String getString () {
  22. return string;
  23. }
  24. public void SetString (string string) {
  25. this.string = string;
  26. }
  27. Public Serializableobject Getobj () {
  28. return obj;
  29. }
  30. public void Setobj (Serializableobject obj) {
  31. This.obj = obj;
  32. }
  33. }
  34. Class Serializableobject implements Serializable {
  35. Private static final long serialversionuid = 1L;
  36. }
To implement a deep copy, you need to read the binary input of the current object in the form of a stream, and then write out the object corresponding to the binary data.

Five prototype patterns for Java Design patterns (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.