Cocoa cola-Cocoa Framework prototype, cocoacola
In many object-oriented applications, the creation cost of some objects is too high or too complex. In this case, you can only make minor changes to recreate the same object, to adapt to specific situations in the program, it is really good. A typical scenario is to copy a combination structure, such as a tree structure. It is very difficult to build a tree combination from scratch. Compared with creating a variety of independent classes that are less different from the parent class, this method makes some objects generate their own replicas highly reusable and easier to maintain.
The model applied to the "copy" operation is called the prototype mode. Copying refers to the production of a series of products using the same mold, and the product based on the mold is called the prototype. The prototype determines what the final product looks like. Although the product is replicated using the same mold, some attributes can be slightly different.
Model Diagram
If you want to better use the prototype, it is necessary to have a deep understanding of shortest replication and deep replication. Below I will use the graphical method to intuitively and conveniently parse their similarities and differences
Light Replication
If the object has a pointer type member variable pointing to a resource in the memory, if you just copy the pointer to a new object (copy), then the underlying resources are actually shared by two instances. If:
In the clone operation of ConcretePrototype, the resource pointer value is copied to a new copy. Although the ConcrePrototype instance generates an instance of the same type as its copy, the pointers of the two instances still point to the same resource in the memory. This method only copies pointer values instead of actual resources.
Deep Replication
So what is deep replication? Deep replication not only copies pointer values, but also copies the Resources pointed to by pointers to generate real copies of actual resources in the memory. As follows:
Copying objects in Cocoa Touch
Cocoa Touch Framework provides a protocol for deep replication for NSObject Derived classes. The NSObject subclass must implement the NSCopying protocol and its method:
(id)copyWithZone:(NSZone *)zone
NSObject has an instance method called (id) copy. The default copy method calls [self copyWithZone: nil]. This method must be implemented for subclasses that adopt the NSCopying protocol. Otherwise, an exception is thrown. In iOS, this method keeps a new copy object and returns it. Specifically, the caller of this method is responsible for releasing the returned object.
In most cases, the implementation of deep replication does not seem very complex. The idea is to copy the necessary member variables and resources, pass them to the new instance, and then return the new instance. The trick is to ensure that the resources in the memory are indeed copied, not the pointer value.
Application scenarios
You can consider using the prototype mode in the following scenarios:
① The object to be created should be independent of its type and Creation Method
② The class to be instantiated is determined at runtime
③ Do not want the factory level corresponding to the product level
④. The differences between instances of different classes are only several combinations of States. Therefore, it is more convenient to copy the corresponding number of prototypes than to manually instantiate them.
⑤ Classes are not easy to create. For example, each component can use other components as the combination object of child nodes. It is easier to copy existing composite objects and modify copies.