Design Pattern [1]: Prototype Pattern

Source: Internet
Author: User

During the interview, the interviewer always asked questions about the design model. Although we will inadvertently use the design model when writing the program, we still cannot tell in front of the interview manager, here I have recorded some of the design patterns that I have seen to help me better use them.

[Prototype mode]

Official definition of prototype: "Use a prototype instance to specify the type of the object to be created, and create a new object by copying this prototype ". A simple understanding is to create a new object based on this prototype. This kind of creation refers to deep replication to get a new memory resource, rather than a new pointer reference.

The official definition is always so boring and hard to understand. You can say that this is also a design model. Let's just say that this design pattern: the prototype pattern is for deep replication. The definition can be understood in this way,

 

A single object Dog * dog = new Dog; then you want to get a dog1. You can do this: Dog * dig1 = [dog copy]; Now dog and dog1 are two independent individuals.

 

This is the so-called prototype. Although it seems that there is no need to build a design model, I would like to thank the author of this design model.

[About deep replication]

Classes such as NSString and NSDictionary have implemented the copyWithZone :( NSZone *) zone method and can be called directly using [NSString copy. The copied copy can be divided into mutable copy and immutable copy ). Generally, immutable copies are returned in the copyWithZone method specified by the NSCopying protocol. The mutableCopyWithZone method of NSMutableCopying returns a mutable copy, and then the copy and mutableCopy methods are called to obtain the copy.

 

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.