Illustrate the role of prototype patterns in the development of IOS applications in design patterns _ios

Source: Internet
Author: User
Tags instance method memory usage shallow copy


1 Preface
in many object-oriented applications, the creation of some objects is too expensive or too complex. It would be much easier to recreate the same objects and make minor changes. We can reuse existing objects with minor changes to accommodate specific situations in the program. Today we are going to learn the pattern.



2 Details
2.1 Definition
the pattern applied to the copy operation becomes a prototype (Prototype) pattern. Reproduction (cloning) refers to the production of a series of products with the same mold. The objects on which the mold is based are called prototypes. Although the product is copied with the same stencil, some properties, such as color and size, can be slightly different, but they still belong to the same class.
2.2 When is the prototype mode
(1) The object that needs to be created should be independent of its type and the way it was created.
(2) The class to instantiate is determined at run time.
(3) Do not want to correspond with the product level of the factory level.
(4) The difference between instances of different classes is only a few combinations of States. Therefore, it is more convenient to copy the corresponding number of prototypes than to instantiate them manually.
(5) Classes are not easy to create, for example, each component can use other components as a grouping object for child nodes. It is easier to copy existing grouped objects and make modifications to the replicas.
The minimum of this pattern is to generate a true copy of the object to be used as the basis (prototype) for other related things in the same environment.
2.3 Shallow copy and deep copy
deep Replication is the opening of new memory to achieve real memory replication, shallow copy, only copy pointers, heap memory unchanged. When we design the system, sometimes some objects need to be based on user operations to complete the copy backup operations, at this time, if the original method to initialize the object will bring some inconvenience and problems:
(1) Some properties of the object are generated in the process of user operation, and cannot be assigned by a initxxx method.
(2) Conventional assignment is too cumbersome and destroys encapsulation.
At this time the advantages of the prototype model will be reflected.



3.Demo
First, create a player class with 2 properties Highestlevel and CurrentLevel, and 2 public methods to modify these 2 properties. The code is as follows:


code as follows:

@interface Player:nsobject <NSCopying>
/**
* Update Player ' s current level during game
*
* @param level
*/
-(void) Updatecurrentlevel: (Nsinteger) level;



/**
* Update Player ' s highest level during game
*
* @param level
*/
-(void) Updatehighestlevel: (Nsinteger) level;

@end


Most crucially, the player needs to implement the Nscopying protocol:
 code as follows:

#pragma mark-override
-(Instancetype) Copywithzone: (Nszone *) zone
{
Player *copyplayer = [[[Self class] allocwithzone:zone] init];
Copyplayer.highestlevel = Self.highestlevel;
Copyplayer.currentlevel = Self.currentlevel;


return copyplayer;
}


Here you see the Nszone type, what type is this? In fact, it is a structure, is to prevent memory fragmentation and introduced a structure. Nszone will allocate memory based on the amount of memory you want to open, and improve memory management. However, the official programming with the ARC release note also points out that the current runtime system ignores the concept of the zone, because its memory management has been very efficient, using zone will instead reduce memory usage, access efficiency, Increase source code complexity, and so on. So generally do not use Nszone, and in this example, although the use of the Allocwithzone method, but we go in to look at the source code will find: Apple in fact, or with the general initialization method instead of the original zone open:
 code as follows:

#pragma mark-override
-(Instancetype) Copywithzone: (Nszone *) zone
+ (Instancetype) Allocwithzone: (struct _nszone *) zone objc_swift_unavailable ("Use Object
initializers instead ");

The prototype design pattern is basically these, of course our player class can become an interface, let subclasses to implement, better embodiment of interface-oriented programming.





Results:


2015-09-18 21:30:32.072 dp_prototype[1173:280693] <Player:0x14d513f60>
2015-09-18 21:30:32.073 dp_ PROTOTYPE[1173:280693] <Player:0x14d5337e0>


When you call the Copy method in another file, you can see that the system is a new piece of memory for us, with a reference count of 1.



Object replication in the 4.Cocoa touch frame
The Cocoatouch Framework provides a protocol that implements deep replication for nsobject derived classes. NSObject subclasses need to 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]. You need to implement this method for subclasses that have adopted the Nscopying protocol, otherwise an exception will be thrown. In iOS, this method keeps the new replica object and then returns it. The caller of this method needs to be responsible for releasing the returned object.
The trick of deep copying is to make sure that the resources in memory are replicated, not just pointers.





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.