Objective-c Class and Factory mode

Source: Internet
Author: User

this article reprinted to http://www.cocoachina.com/ios/20141124/10296.html

I believe we all know about the 23 design patterns mentioned in Gof's "Designing Patterns", which divides the common design patterns into three categories: the creation model, the behavioral pattern, and the structural model. The example of building a hotel in clean Code is that the construction and use of objects in the system should be separated, so it is important to build the object more neatly and conform to the usage scenario.

There is also a way in the System class library of iOS that makes it unnecessary for developers to focus on the specific storage implementations in the class, but to create the right objects based on different requirements scenarios. such as Foudation in the Nsarray, Uikit in the UIButton.

Based on the principle of several plant design patterns, this paper makes a brief collation of the concept of objective-c.

0. Three types of plants

In fact, in addition to the "Design Patterns" mentioned in the Factory method and abstract Factory, often mentioned there is another factory model simple Factory.

In a simple factory, the product has a unified interface, but can have a different implementation, and there is a (usually only one) factory object. When the product is needed, the factory will switch according to the conditions, choose a product realization, build an instance. This design separates the abstraction and implementation of the product, and can communicate with a unified product interface without having to deliberately focus on the specific implementation. The expansion of the product category is also possible, but each additional product needs to modify the factory logic, there is a certain maintenance costs.

Factory method Further, the factory is also abstracted out, interface, to achieve separation. This allows specific factories and specific products to be expanded at the same time without the need to modify existing logic. Of course, the user may be in different scenarios to a certain extent their own corresponding factory selection (this must be known, inevitably).

Abstract factory compared to the factory method is mainly the entire product class system has been horizontally expanded to form a more complete system.

1. Objective-c class family (class Cluster)

iOS development friends must have used NSNumber numberwith ... Method. But it is possible that it is not known that nsnumber such a method call returns an object that is not the NSNumber class itself, which is the subtlety of the Objective-c class family.

As shown, the concept of number is large. And actually NSNumber actually has a lot of hidden subclasses, and we pass NSNumber's numberwith ... The object that the method obtains is the object of its subclass, but for the user it is almost unnecessary to know this, as long as it is OK to know that it is a NSNumber object.

"Simple Concept and Simple Interface", this is the Apple design class family's original intention, also is the class family's merit. It can be imagined that we are going to use integers as parameters to get a NSNumber object and a Boolean parameter to get the NSNumber object is different, which is slightly similar to the switch logic (although through different methods), according to different conditions to provide different sub-class objects, And all of this is centrally declared in the public interface class NSNumber. It is easy to associate with the simple Factory design pattern mentioned above.

Yes, similar to a simple factory, a disadvantage of the class family is also revealed, that is, the existing class family does not expand. For example, if you want to nsnumber support one more situation, this is probably very difficult. Fortunately, the class libraries of these systems have already taken most of them into account and are well thought out, and usually you just have to use them.

2. Subclass extension of class family

Understanding the concept of class family, we can also use its advantages in actual development. It is difficult to extend the subclass of an existing class, but it does not mean that nsnumber, Nsarray and other classes will not be able to inherit. They can still have a custom subclass.

Now that you want to be a subclass of a class, you should:

· The common "abstract" class is a parent class, such as NSNumber, Nsarray, and so on, not its subclasses

· Provide custom storage

· overriding (overriding) all initialization methods of the parent class

· Overriding the "original" method in the parent class

The 2nd most important, the system class family is usually in the parent class only provides a variety of method declarations, and does not provide storage itself, so to customize the subclass must provide its own storage, generally this is the meaning of the custom subclass.

Override the initialization method to conform to the specification of the OBJECTIVE-C initialization chain.

and rewrite the "primitive" method, this to say. According to Apple's documentation, similar to specifying the initialization method, many of the methods within these classes can be divided into two classes, the "primitive" method and the "derivation" method. The "primitive" method defines the most basic behavior of the class and the object, and the "derived" method is based on these "primitive" methods for a more complex logic wrapper. Therefore, the "primitive" method is rewritten, and the "derivative" method also has a different natural effect.

In addition to customizing the subclass, Apple's official recommendation is that developers use a combination of packages to package the class races.

3. Judgment of the class to which the object belongs

Some people will ask, if I do not have special needs, do not need to write Nsarray, NSNumber sub-class, is not to understand the class family is not much significance. Remember here, by understanding the concept of class family, we know at least that the objects obtained by NSNumber are not necessarily (basically not) objects of the NSNumber class itself.

You can experiment with the class of objects that are obtained by [NSNumber Numberwithint:2] and [NSNumber Numberwithbool:yes], one is __nscfnumber and the other is __nscfboolean.
Then, the following judgment is not:

1234 id maybeAnArray = /* ... */;if([maybeAnArray class] == [NSArray class]) {   // Will never be hit}

The use of Ismemberofclass and iskindofclass is required in appropriate cases.

This is how this article is organized so much, more on the following:

Classclusters

The 9th article of effective objective-c

Objective-c Class and Factory mode

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.