1. What is prototype mode? (what)
Prototype mode is a creation design pattern, it is a copy of the way to quickly create an object, there are two ways to copy: 1.) Shallow copy (pointer copy); 2.) deep copy (value copy); Creating a new object with prototype mode is more efficient.
what are deep and shallow copies? (what)
A deep copy is also called a value copy, and the newly created object will open up new space in memory and copy the value of the copied object; a shallow copy is also called a pointer copy, and the pointer to the newly created object points to the same memory space as the original object, with the same value, and the shallow copy object is like the shadow of the original object.
Nsstring*str = @ "AAA";
nsstring*strcopy = [str copy];
nsmutablestring*strmcopy = [Str mutablecopy];
NSLog (@ "str object:%@, Address:%p", str, str);
NSLog (@ "Strcopy object:%@, Address:%p", strcopy,strcopy);
NSLog (@ "Strmcopy object:%@, Address:%p", Strmcopy, strmcopy);
2014-10-07 09:28:18.578 prototype_demo[516:303] str object: AAA, Address: 0x100001058
2014-10-07 09:28:18.579 prototype_demo[516:303] Strcopy object: AAA, Address: 0x100001058
2014-10-07 09:28:18.580 prototype_demo[516:303] Strmcopy object: AAA, Address: 0x10010af70
2. What scenario uses the prototype mode? (Where)
1. Object types need to be run at runtime to determine
2. Require a copy of an object in a certain state
3. The difference between objects is very small, using the prototype system to copy an object and then make the necessary modifications
3. How do I implement the prototype model? (how)
1.) Define a person class that contains three property variables:
@interface Person:nsobject <nscopying, nsmutablecopying>
@property (nonatomic, copy) nsmutablestring *name;
@property (nonatomic, copy) Nsstring*sex;
@property (nonatomic, assign) int age;
-(ID) Initwithname: (nsmutablestring *) aName withsex: (NSString *) asex withage: (int) AAge;
2.) Methods that must be implemented in implementing initialization and protocol in the implementation file
-(ID) Initwithname: (nsmutablestring *) aName withsex: (NSString *) asex withage: (int) aage{
if (self = [super init]) {
Self.name = AName;
Self.sex = Asex;
Self.age = AAge;
}
return self;
}
It is important to note that when an object calls the Copy method, it gets an immutable object regardless of whether the object was previously mutable or immutable. Calling Mutablecopy, however, gets a mutable object, regardless of whether the object was previously mutable or immutable.
-(ID) Copywithzone: (Nszone *) zone
{
Person *obj = [[self class] allocwithzone:zone];
Obj.name = [Self.name mutablecopy];
Obj.sex = [self.sex copy];
Obj.age = Self.age;
return obj;
}
-(ID) Mutablecopywithzone: (nszone*) zone{
Person *obj = [[self class] allocwithzone:zone];
Obj.name = [Self.name mutablecopy];
Obj.sex = [self.sex copy];
Obj.age = Self.age;
return obj;
}
3.) Create a new object using the copy and Mutablecopy methods in the main function
nsmutablestring *name = [nsmutablestring stringwithformat:@ "AAA"];
Person *person = [[Person alloc] initwithname:name withsex:@ ' boy ' withage:20];
Person *aperson = [person copy];
NSLog (@ "%@", aperson.name);
Person *bperson = [person mutablecopy];
NSLog (@ "%@", bperson.sex);
demo:http://download.csdn.net/detail/luozhonglan/8008379
*************************************************************************************************************** **************************************************************************
Presented by Lota Grapefruit
iOS common design Patterns-prototype mode