iOS design mode-prototyping

Source: Internet
Author: User

What is prototype mode?

Prototype mode is a very simple design pattern. The client knows the abstract prototype class. At run time, any object of an abstract prototype subclass can be replicated as the client wishes. So you can make multiple instances of the same type without creating them manually. The class diagram that describes the static relationship between them is as follows:


Prototype declares the interface to replicate itself. As a subclass of prototype, Concreteprototype implements the clone operation of concrete replication itself. The client here replicates itself by requesting a prototype, creating a new object.

Prototype mode: Use the prototype instance to specify the kind of object to create and create a new object by copying the prototype.

When do I use prototype mode?

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

@: The class to instantiate is determined at run time.

@: Do not want the factory hierarchy corresponding to the product hierarchy.

@: Differences between instances of a different class are only a few combinations of States. Therefore, copying the corresponding number of prototypes is more convenient than manual instantiation.

@: 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.

The minimum of this pattern is to generate a true copy of the object to be used as the basis for other related things in the same environment.

Shallow copy and deep copy

Shallow copy: Only the pointer is copied, but the actual resource is not copied.

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

Object replication in the Cocoa Touch framework

The Cocoa Touch Framework provides a protocol for implementing deep replication for NSObject derived classes. Subclasses of NSObject need to implement the Nscopying protocol and its methods-(ID) Copywithzone: (Nszone *) zone. NSObject has an instance method called (ID) copy. The default copy method calls [self Copywithzone: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 returns it. The caller of this method is responsible for releasing the returned object.

In most cases, the implementation of a deep copy does not look complicated. The idea is to copy the necessary member variables and resources, pass them to a new instance of the class, return the new instance, and return to the new instance. The trick is to make sure that the resources in memory are copied, not just pointer values.

iOS design mode-prototyping

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.