First, the introduction of the prototype model of the encyclopedia:
Definition: Specifies the kind of object created with the prototype instance, and creates a new object by copying the prototypes. Prototype prototype mode is a creation design pattern, prototype mode allows an object to create another customizable object without knowing the details of how to create it, by passing a prototype object to the object to be created, The objects to be created are created by requesting the prototype object to copy them themselves. What to solve: the main problem is the creation of "certain complex objects", which are often subject to drastic changes due to changes in demand, but they have relatively stable and consistent interfaces. The obvious point is that if there is a complex object, re-create to spend a lot of code or cost this time you can consider using the prototype mode, when you want to create a new instance through the existing instance of a copy, and then modify the different place values under code to explain how to use the first to create a protocol
1 #import<Foundation/Foundation.h>2 3 @protocolPrototypecopyprotocol <NSObject>4 5 @required6 7 /**8 Copy yourself9 Ten @return Returns a copy of the object One */ A- (ID) clone; - - @end
Creating an Object model
Student.h
1 #import<Foundation/Foundation.h>2 #import "PrototypeCopyProtocol.h"3 4 @interfaceStudent:nsobject <PrototypeCopyProtocol>5 6@property (nonatomic, strong) NSString *name;7@property (nonatomic, strong) NSString *Age ;8@property (nonatomic, strong) NSString *score;9@property (nonatomic, strong) NSString *address;Ten One #pragmaMark-prototypecopyprotocol method A- (ID) clone; - - @end
Student.m
1 #import "Student.h"2 3 @implementationStudent4 5- (ID) Clone {6 7Student *stu = [[Selfclass] [alloc] init];8 9Stu.name =Self.name;TenStu.age =Self.age; OneStu.score =Self.score; AStu.address =self.address; - - returnStu; the } - - @end
The following are used in the controller:
1- (void) Viewdidload {2 [Super Viewdidload];3 4 //Create a first student instance5Student *STU1 =[[Student alloc] init];6 7Stu1.name =@"Jackey";8Stu1.age =@" -";9Stu1.score =@" -";TenStu1.score =@"Chongqing"; One A //Create a second student instance -Student *STU2 =[STU1 clone]; - theStu2.name =@"Franky"; - -}
OBJECTIVE-C prototype mode-Simple introduction and use