Design Pattern-prototype pattern (06), design pattern prototype

Source: Internet
Author: User

Design Pattern-prototype pattern (06), design pattern prototype
Definition

Prototype Pattern is a simple design mode. The prototype is Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype. it means to use a prototype instance to specify the type of the object to be created, and create a new object by copying the prototype.

The prototype has three roles:

1. Client role: this role initiates a request to create an object.
2. Prototype: this role is an abstract role, which is usually implemented by a java interface or abstract class and provides all the interfaces required for the specific Prototype class.
3. Concrete Prototype: this role is a Copied object and must implement an abstract Prototype interface.

Java has a built-in cloning mechanism. The object class has a clone () method, which can be used to clone objects. It only takes two steps to clone a class:

1. Implement the Cloneable interface.
2. overwrite the clone () method of the Object to complete Object cloning. Generally, you only need to call the clone () method of the Object. To enable the external to call this type of clone () method, you can change the access modifier to public.

/*** Prototype * indicates the interface required for Prototype replication */public interface Prototype {// clone method Prototype clone ();} /*** specific Prototype factory class **/public class ConcretePrototype implements Prototype {@ Override public Prototype clone () {try {return (Prototype) super. clone ();} catch (CloneNotSupportedException e) {e. printStackTrace (); return null ;}} public class Client {public void operation (Prototype example) {// obtain example Prototype prototype = example. clone ();}}
Advantages of Prototype

1. excellent performance: the prototype mode is used to copy binary streams in the memory, which is better than directly creating an object. Especially when a large number of objects are generated in a loop, the prototype can better reflect its advantages.

2. Escape the constructor constraints. This is both an advantage and a disadvantage. If you copy objects directly in the memory, the constructor will not execute them, so the constraints are reduced. However, you need to consider this in actual application.

Use Cases of Prototype

1. In resource optimization scenarios, many resources need to be digested during class initialization, including data and hardware resources.

2. For scenarios with performance and security requirements, if a new object requires tedious data preparation and access permissions, you can use the prototype mode.

3. in the scenario where an object has multiple modifiers, an object needs to be provided to other objects for access, and each caller may need to modify its value. You can consider copying multiple objects in prototype mode for callers to use.

In actual projects, the prototype mode rarely appears independently, usually along with the factory method mode. The prototype creates an object by using the clone () method, and then the factory method is provided to the caller.

Example

/*** Implements the clone interface and the clone method, which is the key to achieving cloning */public class Mail implements Cloneable {// recipient private String Explorer; // Mail title private String subject; // Title private String appellation; // Mail content private String contxt; // tail private String tail; // constructor public Mail (String subject, string contxt) {this. subject = subject; this. contxt = contxt;} // clone method public Mail clone () {Mail mail = null; try {mail = (Mail) super. clone (); System. out. println (super. toString (); // super indicates the called object} catch (CloneNotSupportedException e) {e. printStackTrace ();} return mail;}/*** call */public class SendMailDemo {Map students = new LinkedHashMap (); public static void main (String [] args) {// create a prototype mail Mail mail = new Mail ("mail title", "mail content"); Mail. setTail (""); SendMailDemo sendMailDemo = new SendMailDemo (); // get all students Map students = sendMailDemo. getStudent (); for (Object name: students. keySet () {// clone Mail cloneMail = mail. clone (); cloneMail. setAppellation (name. toString (); cloneMail. setReceiver (students. get (name ). toString (); sendMailDemo. sendMail (cloneMail) ;}} public Map getStudent () {students. put ("studentone", "1@foxmail.com"); students. put ("studenttwo", "2@foxmail.com"); students. put ("studentthree", "3@foxmail.com"); students. put ("studentfour", "4@foxmail.com"); students. put ("studentfive", "5@foxmail.com"); students. put ("studentsix", "6@foxmail.com"); students. put ("studentseven", "7@foxmail.com"); return students;} public void sendMail (Mail mail) {System. out. println ("title:" + mail. getSubject () + "\ t recipient email:" + mail. getcycler () + "\ t body:" + mail. getAppellation () + mail. getContxt () + "\ t... sent ");}}

Source code

 

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.