--Prototype model

Source: Internet
Author: User
Come late today, because the company Monday a lot of things to do, and I did not sleep well last night, came after nothing to do first sleep a morning. :(Luckily the boss is Hiahia in the meeting. But next time not so lucky, if there is the Sun Monkey's avatar Law, cloning a random I put in there work, I sleep how good ah .... So let's introduce the prototype model today (prototype)
Sometimes the objects we want to create are broadly similar, but differ in the values of some properties, and their initialization is relatively complex and time-consuming, so we can use the prototype pattern. First initializes an instance of a class, and then other instances are created by cloning the first instance. Then, or in the Clone Method (clone) to handle their differences, or by the caller to deal with, it depends on which is more concerned about their differences. If callers do not care about their differences at all, then there should be a copy method to handle their differences and return the instance.
This pattern is written in the < design pattern >:
Use a prototype instance to create a kind of object, and create a new object by copying the prototypes.
His applicability:
When a system should be independent with the creation, composition, and presentation of his product. (That is, the caller doesn't care how the object being invoked is created, what the internal structure is, and these are handled by the cloning method)
To avoid creating a factory class hierarchy that is parallel to the product class hierarchy (we introduced the factory method earlier, a factory class can have multiple factory methods that create different products, and when a factory class has only one factory method, the interface between the factory class and its products is at a parallel level, In order to avoid creating too many factory classes, the structure is complex and can be in prototype mode.
When the vision of a class can only have one of several different combinations of states, creating a corresponding number of prototypes and cloning them may be more convenient than using the appropriate state to manually spend the class in the city (this is the so-called initialization problem mentioned above)






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.