"Onlookers" design mode (10)--Create prototype mode (Prototype pattern)

Source: Internet
Author: User

The prototype pattern is one of the creation patterns, which is characterized by the "copying" of an existing instance to return a new instance, rather than a new instance. The copied instance is what we call the "prototype", which is customizable.

Prototype patterns are used to create complex or time-consuming instances, because, in this case, copying an existing instance makes the program run more efficiently, or creating values equal, but naming different homogeneous data. ----WIKIPEDIA


First of all, look at an example below, using my self-black, implement the Clonable interface and then implement its Clone method, and then through this method to clone the object. See if we can create new objects as we expect.


public class WangYang implements Cloneable{private string Name;private string Address;public string GetName () {return name ;} public void SetName (String name) {this.name = name;} Public String getaddress () {return address;} public void setaddress (String address) {this.address = address;} @Overrideprotected WangYang Clone () {WangYang WY = null;try {WY = (WangYang) super.clone ();} catch (Clonenotsupportedexcep tion e) {e.printstacktrace ();} return WY;} @Overridepublic String toString () {return "WangYang [name=" + name + ", address=" + address + "]";}}

Test class for testing the above program:

public static void Main (string[] args) {WangYang wy = new WangYang (); Wy.setaddress ("SH"); Wy.setname ("WY"); WangYang WangYang = Wy.clone (); System.out.println (Wangyang = = WY); System.out.println (Wangyang);}

The result of the output is:

From here you can see that the Wangyang object and the WY object are not the same object and re-created the new object.


But a little change, I have a dress, clothes have color properties, then the previous Wangyang class to add a property that is the object of clothing

private string Name;private string address;private Clothes cloth;

after the change, run the main test program again, and the result is that cloth is not copied

System.out.println (wangyang.getcloth () = = Wy.getcloth ());

The result of the output is: true


Next, change the clothes.

private string Name;private string address;private Clothes cloth;private arraylist<clothes> clotheslist = new Array List<clothes> ();

Similar to the above, test in a test class

System.out.println (Wangyang = = WY); System.out.println (wangyang.getclotheslist () = = Wy.getclotheslist ()); System.out.println (wangyang.getcloth () = = Wy.getcloth ()); System.out.println (Wangyang);

Results of the output

Look at the middle of these two true means that the cloth and clotheslist in the WY object are not copied, directly referencing the original object. This place is a shallow copy. The primary copy of the base data type and string.


So how to make a deep copy of it.

The Clone method in the Wangyang class is modified to explicitly clone its inner object.

Deep copy WangYang WY = null;try {WY = (WangYang) super.clone (); cloth = This.cloth.clone (); clotheslist = (arraylist<clothes >) This.clothesList.clone ();} catch (Clonenotsupportedexception e) {e.printstacktrace ();} return WY;

This place wants to let cloth object also can clone, then need to let clothes this class implement Clonable interface implement clone method simultaneously.


What is the test result at this time:

System.out.println (Wangyang = = WY); System.out.println (wangyang.getclotheslist () = = Wy.getclotheslist ()); System.out.println (wangyang.getcloth () = = Wy.getcloth ()); System.out.println (Wangyang);

The two lines in the middle are all false. This indicates that a deep copy was made.



Advantages of Prototype mode

prototype patterns are used to create complex or time-consuming instances, because, in this case, copying an existing instance makes the program run more efficiently, or creating values equal, but naming different homogeneous data .


Attention:

1. The prototype schema creation object does not call the constructor to create it. Directly from memory to assign the data of the object, ignoring the constraints of the constructor, he and the previously mentioned Singleton mode has a conflict, the singleton mode will set the constructor to private, and the prototype mode can bypass the constructor to go through the object in memory data area to create objects, so this place will conflict.

2. Note Deep copy and shallow copy. Deep copies may be required for arrays, container objects, objects, and so on.


Design Pattern Series code: Design pattern Series code download

"Onlookers" design mode (10)--Create prototype mode (Prototype pattern)

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.