iOS design mode-prototype mode

Source: Internet
Author: User
Tags shallow copy

Prototype mode is a relatively simple design pattern, said simply, is to copy objects.

The prototype pattern is considered in the following cases:

1. Objects that need to be created should be independent of their type and how they are 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. Differences between instances of different classes are only a few combinations of States

5, the class is not easy to create, copy the existing composite objects to modify the copy is easier

Deep copy and shallow copy

Shallow copy: Only the value of the pointer is copied and the actual resource is not copied. Shallow replication is very common in our daily development

1UILabel * label = [[UILabel alloc] Initwithframe:cgrectmake (0,0, -, -)];2Label.text =@"Hello";3     4     //label2 Shallow copy label, two pointers pointing at the same memory space5UILabel * Label2 =label;6     7NSLog (@"%p,%p", label, Label2);

Output Result:

Deep copy: Not only copies the pointer, but also copies the resource pointed to by the pointer.

In most cases, the implementation of a deep copy does not look complicated. Copies the required member variables and resources to a new instance of this class.

The Cocoa Touch Framework provides a protocol--nscopying protocol that implements deep replication for nsobject derived classes.
The subclass of the Nscoping protocol needs to be called-(ID) Copywithzone: (Nszone *) zone this way, otherwise an exception will occur. =
NSObject has an instance method (ID) copy that calls [self copywithzone:nil] by default.

1 //JZViewModel.h2 3 #import<Foundation/Foundation.h>4 5 /**6 The *cocoa Touch Framework provides a protocol--nscopying protocol that implements deep replication for nsobject derived classes. 7 * The subclass of the Nscoping protocol is adopted, it needs to call-(ID) Copywithzone: (Nszone *) Zone This method, otherwise the exception will occur. =8 *nsobject has an instance method (ID) copy that calls [self copywithzone:nil] by default. 9  */Ten @interfaceJzviewmodel:nsobject <NSCopying> One  A@property (nonatomic, copy) NSString *name; -@property (nonatomic, copy) NSString *Age ; -  the- (ID) Initwithname: (NSString *) name Andage: (NSString *) age; -  - @end
1 //JZVIEWMODEL.M2 3 #import "JZViewModel.h"4 5 @implementationJzviewmodel6 7 //Initialize Method8- (ID) Initwithname: (NSString *) name Andage: (NSString *) Age9 {TenSelf =[Super init]; One      A     if(self) { -_name =name; -_age =Age ; the     } -      -     returnSelf ; - } +  - //Copy +- (ID) Copywithzone: (Nszone *) Zone A { at     //generates a new object and initializes the -Jzviewmodel * ViewModel = [[Selfclass] Allocwithzone:zone] initWithName:self.name andAge:self.age]; -      -Viewmodel.name =Self.name; -Viewmodel.age =Self.age; -      in     returnViewModel; - } to  +-(NSString *) Description - { the     return[NSString stringWithFormat:@"name:%@, age:%@", Self.name, self.age]; * } $ Panax Notoginseng @end
1 //VIEWCONTROLLER.M2 3 #import "ViewController.h"4 #import "JZViewModel.h"5 6 @interfaceViewcontroller ()7 8 @end9 Ten @implementationViewcontroller One  A- (void) Viewdidload { - [Super Viewdidload]; -      theJzviewmodel * ViewModel = [[Jzviewmodel alloc] Initwithname:@"Xiao Ming"Andage:@" -"]; -NSLog (@"%@", ViewModel); -NSLog (@"ViewModel =%p", ViewModel); -      +Jzviewmodel * ViewModel2 =[ViewModel copy]; -NSLog (@"%@", ViewModel2); +NSLog (@"ViewModel2 =%p", ViewModel2); A      at } -  - @end

Operation Result:

iOS design mode-prototype mode

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.