Design Pattern learning Summary (5) prototype Pattern

Source: Internet
Author: User

This series mainly records what the design pattern intends to be, what issues it wants to solve, and when it can be used; how it solves it, master its structure diagram, remember its key code; be able to think of at least two of its application instances, one in life and one in software; What are the advantages and disadvantages of this model, and what use scenarios are there, what should I pay attention to when using it.

5. prototype mode

Purpose: Use a prototype instance to specify the object type and copy the prototype to create a new object.

Major solution: Create and delete prototypes during runtime

When to use: 1. when a system should be independent of its product creation, composition, and representation; 2. when the class to be instantiated is specified at the runtime, for example, through dynamic loading; 3. to avoid creating a factory class level parallel to the product class level; 4. when an instance of a class can only have one of several combinations of different States. Creating prototype numbers and cloning them may be more convenient than manually instantiating this type with the appropriate state each time.

How to solve: use an existing prototype object to quickly generate instances that are the same as the prototype object

Structure:

Key code: 1. implement the clone operation, inherit Cloneable in JAVA, rewrite clone (), and. NET, you can use the MemberwiseClone () method of the Object class to realize the Object's superficial copy or the deep copy through serialization. 2. The Prototype mode is also used to isolate the coupling between the users of class objects and specific types (variable classes). It also requires that these "variable classes" have stable interfaces.

Application Example: 1. Cell Division 2. Object clone () method in JAVA

Advantages: 1. Performance Improvement 2. Escape constructor Constraints

Disadvantages: 1. the function of the class should be considered comprehensively when the cloning method is configured, which is not difficult for the new class, but not necessarily easy for the existing class, especially when a class reference does not support serialized indirect objects, or references contain cyclic structures. 2. Cloneable Interface 3 must be implemented to avoid constructor Constraints

Use Cases: 1. Resource Optimization scenarios 2. class initialization requires a lot of resources to be digested, including data and hardware resources. 3. scenarios requiring performance and security 4. Using new to generate an object requires tedious data preparation or access permissions, you can use the prototype mode. 5. scenario 6: Multiple modifier of an object. when an object needs to be accessed by other objects and each caller may need to modify its value, you can consider copying multiple objects in prototype mode for the caller to use. 7. In actual projects, the prototype mode rarely appears independently. Generally, it appears together with the factory method mode. Create an object by using the clone method and then provide the factory method to the caller. The prototype has been integrated with Java and can be easily used.

Note: Unlike constructing a new object by instantiating a class, the prototype generates a new object by copying an existing object. implement Cloneable, rewrite, and deep copy to read binary streams through Serializable,

Related Article

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.