Rehash series: Design pattern prototype mode

Source: Internet
Author: User
Tags rehash

Abstract: Original creation Place: http://www.cnblogs.com/Alandre/sediment brick slurry Carpenter hope reprint, keep abstract, thank you!

Dear me, filial piety is difficult, Fang me, filial piety. First, what is prototype mode

Prototype mode is an object-creation pattern that takes the method of copying a prototype object to create an instance of an object. Instances created using the prototype pattern have the same data as the prototype.

Second, the characteristics of the prototype model
    • 1. The target object is created by the prototype object itself. That is, object creation is an action that originates from the prototype object itself.
    • 2. The target object is a clone of the prototype object. That is, objects created through the prototype pattern have the same structure as the prototype object and have the same value as the prototype object.
    • 3. Based on the different depth levels of object cloning, there are shallow clones and deep clones.
Application scenario and implementation of prototype model
    • -When creating an object, we don't just want the object being created to inherit the basic structure of its base class, but also to inherit the data from the prototype object.
    • -it is expected that changes to the target object will not affect existing prototype objects (deep cloning can be completely unrelated).
    • -Hides the details of the clone operation. Very often, the cloning of the object itself involves the data details of the class itself.

Superficial cloning

ImportJava.util.ArrayList;ImportJava.util.List; Public classPersonImplementscloneable{PrivateString Nanme;PrivateString sex;Private intAgePrivateList<string> friends; PublicString Getnanme () {returnNanme; } Public voidSetnanme (String nanme) { This. Nanme = Nanme; } PublicString Getsex () {returnSex } Public voidSetsex (String Sex) { This. sex = sex; } Public intGetage () {returnAge } Public voidSetage (intAge) { This. Age = Age; } PublicList<string> Getfriends () {returnFriends } Public voidSetfriends (list<string> friends) { This. Friends = friends; } @OverrideprotectedPerson Clone ()throwsclonenotsupportedexception {return(person)Super. Clone (); }    }

MainClass:

ImportJava.util.ArrayList;ImportJava.util.List; Public classmainclass{ Public Static voidMain (string[] args)throwsclonenotsupportedexception {Person person =NewPerson ();        Person.setnanme ("Jeff Lee");        Person.setage (20); Person.setsex ("Boy");//Person person2 = person; reference same change same time changePerson Person2 = Person.clone ();//refer to different content and also change theirSystem.out.println (Person2.getnanme ());        Person2.setnanme ("Jeff Lee 2");        System.out.println (Person2.getnanme ());    System.out.println (Person.getnanme ()); }}

Shallow cloning cannot clone a reference inside an object.

Deep clones simply need to redefine their references to:

@Override    protectedthrows clonenotsupportedexception    {person person        = (person)  Super. Clone ();        New Arraylist<string> ();         for this. getfriends ())        {            friends.add (friend);        }        Person.setfriends (Friends);        return person;     }

Test code:

ImportJava.util.ArrayList;ImportJava.util.List; Public classmainclass{ Public Static voidMain (string[] args)throwsclonenotsupportedexception {Person person =NewPerson (); List<string> friends =NewArraylist<string> ();        Friends.add ("Zhang San");        Friends.add ("Li Si");                Person.setfriends (Friends);                Person Person2 = Person.clone ();        System.out.println (Person.getfriends ());                System.out.println (Person2.getfriends ());        Friends.add ("Wang Wu");                Person.setfriends (Friends);        System.out.println (Person.getfriends ());    System.out.println (Person2.getfriends ()); }}

Iv. thanks to the sources of knowledge and summary

From: Java design mode

If the above articles or links are helpful to you, don't forget to click "Like a" button in the bottom right corner of the article button or the page. You can also click on the right side of the page to "share" the hover button Oh, let a lot of other people read this article.

have a kiss to make more pleasant my color soft my voice to the joy of the cry with the tart no resentment

Rehash series: Design pattern 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.