Prototype pattern prototype

Source: Internet
Author: User
7.6 prototype mode Summary

Prototype is a method for quickly creating a large number of identical or similar objects. It is widely used in software development. Many software products provide copy (CTRL + C) and paste (CTRL + V) the operation is a typical application of the prototype mode. The following briefly summarizes the effects and applicability of the mode.

1. Main advantages

The main advantages of the prototype mode are as follows:

(1) When creating a new object instance is complex, you can use the prototype mode to simplify the object creation process and copy an existing instance to improve the efficiency of creating a new instance.

(2) good scalability. Because the abstract prototype class is provided in the prototype mode, you can program the abstract prototype class on the client, and write the specific prototype class in the configuration file, adding or removing product categories has no impact on the original system.

(3) The prototype mode provides a simplified creation structure. The factory method mode usually requires a factory level structure that is the same as the product class level structure, which is not required in the prototype mode, in prototype mode, product replication is implemented by cloning methods encapsulated in the prototype class, and no special factory class is required to create the product.

(4) The object state can be saved by means of deep cloning. In prototype mode, the object is copied and its state is saved, it can be used as needed (such as restoring to a certain historical State) to assist in undo operations.

2. Main disadvantages

The main disadvantages of the prototype mode are as follows:

(1) You need to configure a clone method for each class, And the clone method is located inside the class. When modifying an existing class, you need to modify the source code, it violates the "open and closed principle ".

(2) complex code is required for deep cloning. When multiple nested references exist between objects, the classes corresponding to each layer of objects must support deep cloning, which may be difficult to implement.

3. Applicable scenarios

You can consider using the prototype mode in the following cases:

(1) it is costly to create a new object (for example, initialization takes a long time and occupies too many CPU or network resources ), the new object can be obtained through copying existing objects in prototype mode. If it is a similar object, You Can slightly modify its member variables.

(2) If the system wants to save the object state, but the object state changes little, or the object itself occupies less memory, you can use the prototype mode with the memorandum mode.

(3) Avoid using hierarchical factory classes to create hierarchical objects, and the instance objects of the class have only one or a few combined states, it is easier to get a new instance by copying the prototype object than creating a new instance by using the constructor.

Prototype pattern prototype

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.