1 Preface
In many object-oriented applications, the creation cost of some objects is too large or too complex. It would be much easier to recreate the same object and make minor changes. We can reuse existing objects through slight changes to adapt to specific situations in the program. Today, let's take a look at this model.
2. Details
2.1 Definition
The Mode Applied to the copy operation becomes the Prototype mode. Cloning refers to the production of a series of products using the same mold. An item based on a mold is called a prototype. Although the product is replicated using the same mold, some properties, such as color and size, can be slightly different, but they still belong to the same category.
2.2 When is the prototype
(1) the object to be created should be independent of its type and creation method.
(2) the class to be instantiated is determined at runtime.
(3) do not want the factory level corresponding to the product level.
(4) The differences between instances of different classes are only several combinations of States. Therefore, copying the corresponding number of prototypes is more convenient than manual instantiation.
(5) classes are not easy to create. For example, each component can use other components as a combination object of subnodes. It is easier to copy existing composite objects and modify copies.
This mode is used to generate a real copy of an object to serve as the basis (prototype) for other related things in the same environment ).
2.3 light replication and deep Replication
If an object has a pointer-type member variable pointing to a resource in the memory, what if we copy this object? Pointers are only placeholders for storing Chinese source addresses in the memory. If the pointer is copied to a new object (copy) during the replication operation, the underlying resources are shared by two instances on the shelf.
Therefore, only the pointer instead of the actual resource is copied.
Deep Replication refers to not only copying pointer values, but also copying Resources pointed to by pointers. It is not just a simple copy of the resource pointer, but also a real copy of the actual resources in the memory. Therefore, the pointer of the copy object points to a copy of the unified resource (content) in different locations in the memory.
2.4 object replication in the Cocoa Touch framework
The CocoaTouch framework provides a protocol for implementing 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 [selfcopyWithZone: 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. The caller of this method is responsible for releasing the returned object.
The deep Replication Technique is to ensure that resources in the memory are indeed copied, not just pointers.
3 conclusion
The above is all content and I hope it will help you.