AOP programming in Objective-C

Source: Internet
Author: User

AOP programming in Objective-C
AOP programming concept in Objective-C

In the software industry, AOP is short for Aspect Oriented Programming. It is a derivative model of functional Programming. A technology that implements unified maintenance of program functions by means of pre-compilation and dynamic proxies during runtime. Its main functions include logging, Performance Statistics, security control, transaction processing, and exception handling. The main intention is to divide the log records, Performance Statistics, security control, transaction processing, Exception Processing and other codes from the business logic code. By separating these behaviors, we hope that they can be independent into non-guided business logic methods, and thus change these behaviors without affecting the business logic code.

Application

For example, we have a method sumA: andB:, which is used to return a string of the sum of AB. we add a piece of code before and after this method.

Before running the method, we should change the parameter to 2 and 3. Of course, this is for demonstration purposes. Do not change the parameter in actual use, otherwise, other colleagues will swear. After running the method, we will output the passed parameters and returned values.
- (void)clickTestAop:(id)sender{    AopTestM *test = [[AopTestM alloc] init];    NSLog(@"run1");    [test sumA:1 andB:2];        NSString *before = [XYAOP interceptClass:[AopTestM class] beforeExecutingSelector:@selector(sumA:andB:) usingBlock:^(NSInvocation *invocation) {        int a = 3;        int b = 4;                [invocation setArgument:&a atIndex:2];        [invocation setArgument:&b atIndex:3];                NSLog(@"berore fun. a = %d, b = %d", a , b);    }];        NSString *after =  [XYAOP interceptClass:[AopTestM class] afterExecutingSelector:@selector(sumA:andB:) usingBlock:^(NSInvocation *invocation) {        int a;        int b;        NSString *str;                [invocation getArgument:&a atIndex:2];        [invocation getArgument:&b atIndex:3];        [invocation getReturnValue:&str];                NSLog(@"berore fun. a = %d, b = %d, sum = %@", a , b, str);    }];        NSLog(@"run2");    [test sumA:1 andB:2];        [XYAOP removeInterceptorWithIdentifier:before];    [XYAOP removeInterceptorWithIdentifier:after];        NSLog(@"run3");    [test sumA:1 andB:2];} - (NSString *)sumA:(int)a andB:(int)b{    int value = a + b;    NSString *str = [NSString stringWithFormat:@"fun running. sum : %d", value];    NSLog(@"%@", str);        return str;}


When we execute this code, Let's guess what the result is. The result is as follows:

2014-10-28 22:52:47.215 JoinShow[3751:79389] run12014-10-28 22:52:52.744 JoinShow[3751:79389] fun running. sum : 32014-10-28 22:52:52.745 JoinShow[3751:79389] run22014-10-28 22:52:52.745 JoinShow[3751:79389] berore fun. a = 3, b = 42014-10-28 22:52:52.745 JoinShow[3751:79389] fun running. sum : 72014-10-28 22:52:52.745 JoinShow[3751:79389] berore fun. a = 3, b = 4, sum = fun running. sum : 72014-10-28 22:52:52.746 JoinShow[3751:79389] run32014-10-28 22:52:52.746 JoinShow[3751:79389] fun running. sum : 3

Implementation Principle

Use Objective-C's powerful runtime.

We know that when sending a method to an object, if the current class and parent class do not implement this method, the forwarding process will be followed.

Dynamic Method resolution-> fast message forwarding-> Standard Message forwarding

If you are confused, search for "Objective-C message forwarding ".

After learning about message forwarding, we have come to the idea of aop. We will first kill the original method funa, so that when sending methods to objects, we will go through the forwarding process, then we hook up the object's fast message forwarding method, point the object implementing funa to our aop object, and finally execute the before instead after method in the Standard Message forwarding of the aop object.

For specific code, please download it from github. Remember to give us a star.

Link https://github.com/uxyheaven/XYQuickDevelop

Search for XYAOP. h In the code

Related Methods

This section describes some of the runtime methods used.

// Add a new method BOOL class_addMethod (Class cls, SEL name, IMP imp, const char * types) to cls ); // replace one method in cls to implement IMP class_replaceMethod (Class cls, SEL name, IMP imp, const char * types ); // return the specified cls Method class_getInstanceMethod (Class cls, SEL name); // set the implementation of a Method IMP method_setImplementation (Method m, IMP imp ); // return the implementation of the name method in cls IMP class_getMethodImplementation (Class cls, SEL name );


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.