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.