Design Pattern 5-prototype Pattern

Source: Internet
Author: User


1. Interpretation 1.1 Definition

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


1.2 Analysis

The concept description is actually an interface that can copy attributes and create new objects through this interface.


2 code parsing 2.1 reference and create an object

Let's take a look at the following code. This is a simple code for creating objects.

Score score = new score (); person = new person (score); person person2 = (person) person. clone (); // prototype copy person person3 = person; print as follows: Person: [email protected] mname = Zhang San Mmajor = itperson2: [email protected] mname = Zhang San Mmajor = itperson3: [email protected] mname = James Mmajor = it

We found that person3 and person reference the same object, while person2 references the new object. As shown in:

 

Therefore, a new object is created when you use the prototype copy. If you use the "=" value assignment, only the reference address is passed.


2.2 implement the prototype mode

 

First, you need an Interface Class (prototype) that defines the copy interface.

Then a concreteprototype is required to inherit and implement the corresponding copy interface.

Finally, you can call the interface (client) as needed ).

Interface Class (prototype): Public interface prototype {public object clone ();} implementation class (concreteprototype): Public class person implements prototype {Public String mname; Public String Mmajor; public score mscore; Public Person (score pscore) {mname = "zhangsan"; Mmajor = "it"; mscore = pscore;} public object clone () {prototype = new person (mscore); Return prototype ;}} call: score = new score (); person = new person (score); person person2 = (person) person. clone (); // prototype copy


2.3 deep copy and shortest copy

If the prototype class contains objects, such as the person in the instance, if you want the score to be passed not only by reference, but by creating a new object, it is a deep copy. If it is passed only by reference, it is a shallow copy. Compare the following two sections of code to understand.

Shallow copy: Public Person (score pscore) {mname = "zhangsan"; Mmajor = "it"; mscore = pscore;} deep copy: Public personcloneobject (score pscore) {mname = "James"; Mmajor = "it"; mscore = (score) pscore. clone ();}

Code: https://github.com/bird7310/DesignPatternExample.git

Package name: COM. ahacool. designpattern. Prototype


3. Conclusion

It's not easy to write a blog. I admire those who have written many blogs and published books. It is really difficult to write things clearly and write so many articles.

At present, the idea is simple and can clarify things to achieve the goal. In the future, we will work hard on conciseness and clarity.

 

Design Pattern 5-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.