iOS Design Pattern A Brief Introduction to Category
- 2021-07-22 11:37:24
- OfStack
What is Category
The Category pattern is used to extend existing classes by adding methods to them, and in many cases Category is a better choice than creating subclasses. Newly added methods are also automatically inherited by all subclasses of the extended class. When we know that a method in an existing class has BUG, but this class exists in the form of a library, and we cannot directly modify the source code, Category can also be used to replace the entity of a method in this existing class, thus achieving the purpose of repairing BUG. However, there is no easy way to call the replaced method entity in an existing class. It should be noted that when Category is prepared to replace a method, 1 must ensure that all the functions of the original method are realized, otherwise this replacement is meaningless and will lead to a new BUG. Unlike subclasses, Category cannot be used to add instance variables to extended classes. Category is commonly used as a tool for organizing framework code.
Use of Category
1. Extend an existing class without creating an inherited class.
2. Simplify the development of classes (when a class needs to be developed by multiple programmers, Category can put the same class in different source files according to its purpose, which is convenient for programmers to independently develop corresponding method sets).
3. Group the commonly used related methods.
4. Can be used to fix BUG without source code.
Usage of Category
In Obj-C, the method for declaring an Category extension of an existing class is as follows:
@interface ClassName (CategoryName)
-methodName1
-methodName2
@end
The above declaration 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 category extension for the NSString class. File- > New- > File then select Cocoa Touch Objective-C category. Name it ReverseNSString. The system automatically generates a.h and.m files with fixed format ClassName+CategoryName.
Declare Category
Open the NSString+ReverseNSString. h file and add the following code to it:
#import <Foundation/Foundation.h>
@interface NSString (ReverseNSString)
+ (NSString*) reverseString:(NSString*)strSrc;
@end
Implement Category
Implementation of reverseString method in 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, add a button ReverseString in view, and set the corresponding action method as reverseString. Add another label on view, named myString, the default value is "HelloCategory Design Pattern!". Click the button to reverse this 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 efficient decomposition of large classes. Usually, the methods of a large class can be decomposed into different groups according to some logic or correlation. The larger the code amount of a class, the more useful it is to decompose this class into different files, each of which is a collection of some related methods of this class.
When multiple developers work together on a project, each person undertakes the development and maintenance of a separate cagegory. This makes version control easier because there are fewer work conflicts between developers.
Category VS Adding Subclasses
There are no well-defined criteria to guide when to use Category and when to use the method of adding subclasses. However, there are the following guiding suggestions:
1. If you need to add a new variable, you need to add subclasses.
2. If only one new method is added, Category is a better choice.