OBJECTIVE-C Design Pattern-Prototype prototype (object creation)

Source: Internet
Author: User
Tags instance method



1. Prototypes






prototype Design pattern the so-called prototype design pattern, in fact, is the object copy, this feature in all languages are basically exist.



We know that in OC, Object assignment is actually a reference copy of an object, which is actually equivalent to a pointer in C language. A new variable was created, but it still points to the same piece of memory address.



So once a reference changes the properties of the object, all references to that object will change.



Sometimes we don't like to do this, and we want to recreate an object that is exactly the same as an assignment object. For example, when we were writing a game, we created a enemy object and customized its complex walking path, and we wanted to create one at a time. But when you create it again, all the creation will have to be re-written again, which is very inconvenient. For example, we created a tree structure, and if we wanted to re-create the same object, the cost would be very large, and we would need to use the prototype design pattern to replicate the object.






When to use



1. The object that needs to be created should be independent of its type and how it was created.



This means that the object we want is not created directly from the initialization function, and its creation process is not universal and complex.



2. To instantiate a class is determined at run time.



When writing code, it is not known which object will be created and how complex the internal structure (e.g., complexity depends on the user's actions)



3. Do not want the factory level corresponding to the product hierarchy



Do not use factory methods or abstract factories to control the product creation process, want to copy objects directly



4. The differences between instances of different classes are only a few 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 composite object for child nodes. It is easier to copy an existing composition object and make modifications to the copy.



If the internal structure is complex, it is not easy to reproduce.






The following two common usage scenarios



1. There are many related classes, their behavior is slightly different, and the main difference is the internal properties, such as name, image, etc.;



2. You need to use a composite (tree) object as the basis for something else, such as using a composite object as a component to build another composite object.






Shallow copy and deep copy



Shallow copy: Copies only pointers and not internal objects.



Deep copy: That is, copy the pointer and re-open the memory space for the internal object to be copied separately.






OC Object Replication



Deep replication of OC requires implementation of Nscopying protocol and its methods



-(ID) Copywithzone: (Nszone *) zone.



NSObject has an instance method that is (ID) copy.



Default Copy method call [self copy withzone:nil]



For subclasses that have adopted the Nscopying protocol, this method needs to be implemented, otherwise an exception will be thrown. In iOS, this method keeps the new copy object and then returns it.



In most cases, deep replication is not complicated, and the idea is to copy the necessary member variables and resources, pass them to a new instance of the class, and return to the new type.



Demo


Prototype is a class that implements the NSCopying protocol and implements (id) copyWithZone: (NSZone *) zone method.
  1     
  2 Prototype * type1 = [Prototype new];
  3 type1.name = @ "123";
  4 Prototype * type2 = type1;
  5 if (type1.name == type2.name)
  6 NSLog (@ "% p", & type1);
  7 Prototype * type3 = [type1 copy];
  8 if (type1.name == type3.name)
  9 NSLog (@ "% p", & type3);
10 type1.name [email protected] "456";
11 NSLog (@ "% @% @", type2.name, type3.name);
12
13 / *
14 2015-07-19 21: 15: 13.618 Prototype [31679: 8185916] 0x7fff5fbff7e8
15 2015-07-19 21: 15: 13.619 Prototype [31679: 8185916] 0x7fff5fbff7d8
16 2015-07-19 21: 15: 13.619 Prototype [31679: 8185916] 456 123
17 * / 


As can be seen, the Type1 property changes, the shallow copy of the type2 also changed, but Type3 still retain the original value to prove that it is a deep copy.



Objective-c design mode-prototype prototype (object creation)


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.