| Introduction Definition: Use the prototype instance to specify the kind of object to create, and create new objects by copying the prototypes.
Create more objects of the same type by deep copying (cloning, copying) An object of a specified type. This specified object can be referred to as a "prototype" object, which means that more objects of the same type are obtained by copying the prototype object. Is the prototype design pattern. Use the Copy protocol in iOS to complete this process.
Prototype mode is mainly used for deep copy of object, its core is the prototype class prototype in class diagram.
The core idea of prototype mode is to close the process of copying to the internal completion of the object. The first benefit is to modify the replication process without affecting the external consumer.
Advantages and usage scenarios of prototype mode
- simplifies the creation of objects and encloses the process of creating them inside the object. You might consider using the prototype pattern when you need to create similar objects repeatedly. For example, to create objects in a loop, if the object creation process is complex or a lot of cycles, using prototype mode can simplify the creation process, and can improve the overall performance of the system.
- Because the replication process is closed, this reduces the coupling to the client and improves stability. If the replication process is completely transparent to the client, it undoubtedly increases the client's responsibility, so once the replication process is modified, the stability of the client will inevitably be affected (the client may be modified by the bug).
- Centrally created for easy management. Because the created procedure is closed inside the object, just modify the inside of the object and all calls to the deep copy take effect.
Summarize:
Design principles conforming to the prototype pattern:
1. Single responsibility principle. The client only needs to focus on its own business and not be responsible for the replication process.
2. Dimitri rule (least known principle). Prototype hides the process of copying, reducing coupling. and prototype only need to conform to the copying protocol, the client does not need to know which prototype is the specific class, this benefit is easy to derive.
Examples of optional shares:
The data section is a lot of examples, there are no figures listed here. For example, profit and loss data
Cprofitlosssummaryhistorydata.
if
iOS design mode-prototype mode