Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement the java prototype pattern

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to implement the java prototype pattern". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to realize the java prototype pattern.

Definition:

Create a new object instance by copying an existing object instance.

Achieve:

Implement the Cloneable interface:

The function of the Cloneable interface is to inform the virtual machine at run time that the clone method can be safely used on the class that implements this interface. In the java virtual machine, only classes that implement this interface can be copied, otherwise a CloneNotSupportedException exception will be thrown at run time.

Override the clone method in the Object class:

In Java, the parent class of all classes is the Object class, and there is a clone method in the Object class, which returns a copy of the object, but its scope is of protected type, and the general class cannot be called, so the prototype class needs to change the scope of the clone method to the public type.

Example:

For example, for sending an invitation from an email, most of the content of the email is the same: the reason for the invitation, the place of the invitation, the time of gathering, etc., but the name of the invitee and the e-mail address sent are different.

Define the Mail class:

Public class Mail implements Cloneable {

Private String receiver

Private String subject

Private String content

Private String tail

Public Mail (EventTemplate et) {

This.tail = et.geteventContent ()

This.subject = et.geteventSubject ();}

@ Override public Mail clone () {Mail mail = null

Try {mail = (Mail) super.clone ();} catch (CloneNotSupportedException e) {

/ / TODO Auto-generated catch block e.printStackTrace ();} return mail;}

/ / get 、 set.

}

Test method:

Public static void main (String [] args) {

Int I = 0

Int MAX_COUNT = 10; EventTemplate et = new EventTemplate ("invitation letter (unchanged)", "wedding birthday, etc. (unchanged part)"; Mail mail = new Mail (et)

While (I < MAX_COUNT) {Mail cloneMail = mail.clone (); cloneMail.setContent ("Mr. XXX (madam) (change)"

+ mail.getTail (); cloneMail.setReceiver ("everyone's email address... com (changes)"); sendMail (cloneMail); iTunes;}}

Advantages:

1. Creating an object using a prototype model is more efficient than directly new an object, because it directly manipulates binary streams in memory, especially when copying large objects, the performance difference is very obvious.

2. Hides the complexity of creating new instances, making it as easy to create objects as we copy and paste when editing documents.

Disadvantages:

1. Because the constructor of the class is not called when the object is copied using the prototype pattern, the prototype pattern cannot be used in combination with the singleton pattern, because the prototype class needs to change the scope of the clone method to the public type, so the condition of the singleton pattern cannot be satisfied.

2. There can be no final objects when using prototype mode.

3The clone method of the object class only copies the basic data types in the object, while arrays, reference objects, etc., can only be copied separately. The concepts of deep copy and shallow copy are involved here.

Deep copy and shallow copy:

Shallow copy:

After copying an object, variables of the basic data type are recreated, while the reference type points to what the original object points to (which is not safe).

Deep copy:

After copying an object, both the basic data type and the reference type are recreated.

So how to implement deep copy?

Continue the above example by adding an ArrayList attribute.

Private String receiver;private String subject;private String content;private String tail

Private ArrayList ars

At this point, single mail = (Mail) super.clone (); the address area pointed to by ars cannot be changed, and must be copied separately:

Try {mail = (Mail) super.clone ()

Mail.ars = (ArrayList) this.ars.clone ();} catch (CloneNotSupportedException e) {e.printStackTrace ();}

Applicable scenarios:

1. Copy the structure and data of the object.

2. It is hoped that the modification of the target object will not affect the existing prototype object.

3, the cost of creating an object is relatively high.

Thank you for your reading, the above is the content of "how to implement the java prototype pattern". After the study of this article, I believe you have a deeper understanding of how to achieve the java prototype pattern, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report