IOS design model-category

Source: Internet
Author: User
What is category?

The category mode is used to add methods to existing classes to expand existing classes. In many cases, category is a better choice than creating subclass. The newly added method will also be automatically inherited by all subclasses of the extended class. When we know that a method in an existing class has a bug, but this class exists in the form of a library, we cannot directly modify the source code, category can also be used to replace the entity of a method in this existing class to fix bugs. However, there is no convenient way to call the replaced method entity in the existing class. Note that when you prepare category to replace a method, you must ensure that all functions of the original method are implemented. Otherwise, such substitution is meaningless and will cause new bugs. Unlike subclass, category cannot be used to add instance variables to extended classes. Category is usually used as a tool for organizing framework code.

Use of category

1. extend existing classes without creating inheritance classes.

2. simplify the development of classes (when a class requires the collaborative development of multiple programmers, category can place the same class in different source files according to their purposes, so that programmers can develop a set of corresponding methods independently ).

3. Group common related methods.

4. It can be used to fix bugs without source code.

CATEGORY usage

In obj-C, the following method is used to declare the category extension of an existing class:

@interface ClassName (CategoryName)-methodName1-methodName2@end

The above statement is usually in the. h file, and then we implement these methods in the. M file:

@implementation ClassName (CategoryName)-methodName1-methodName2@end

We create an iOS single view applciation named categoryexample. Then, create an nsstring class category extension. File-> New-> file and then select cocoa touch objective-C category. Name it reversensstring. The system will automatically generate a. h and. M files with a fixed format of classname + categoryname.

Declare category

Open the nsstring + reversensstring. h file and add the following code:

#import <Foundation/Foundation.h>@interface NSString (ReverseNSString)+ (NSString*) reverseString:(NSString*)strSrc;@end

Implement category

The reversestring method is implemented in the nsstring + reversensstring. M file:

#import"NSString+ReverseNSString.h"@implementationNSString (ReverseNSString)+ (NSString*)reverseString:(NSString*)strSrc;{    NSMutableString *reversedString =[[NSMutableString alloc]init];    NSInteger charIndex = [strSrc length];    while (charIndex > 0) {        charIndex--;        NSRange subStrRange =NSMakeRange(charIndex, 1);        [reversedString appendString:[strSrcsubstringWithRange:subStrRange]];    }    return reversedString;}@end

The rest of the work is to verify our category. In the view, add a button reversestring and set the corresponding action method to reversestring. add a label on the view and name it mystring. The default value is "hellocategory design pattern! ". Click the button to reverse the string. The main code is as follows:

-(IBAction)reverseString:(id)sender {    NSString *test = [NSStringreverseString:_myString.text];    _myString.text = test;   }
Code Organization

Category is used for effective decomposition of large classes. Generally, a large class method can be divided into different groups based on a certain logic or correlation. The larger the amount of code for a class, the more useful it will be to break this class into different files, each file is a collection of some related methods of this class.

When multiple developers work together to complete a project, each person is responsible for the development and maintenance of a separate cagegory. In this way, Version Control is simpler, because there are fewer conflicts between developers.

CATEGORY vs add subclass

There is no well-defined criteria for determining when to use category and when to add subclasses. However, there are several guiding suggestions:

  1. To add a new variable, add a subclass.
  2. If you only want to add a new method, it is better to use category.

 

If there are any mistakes in this article, please correct them and make progress together. Thank you!

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.