OC learning --- proxy mode, oc --- proxy

Source: Internet
Author: User

OC learning --- proxy mode, oc --- proxy

In the previous article, we introduced the concept of the Protocol in OC: Protocol.

Here is a simple example:

Children, nurses, and nannies. There are two methods for children: wash and play.

Here, the proxy objects are nurses, nannies, and children.

Take a look at the Code:

First, let's take a look at the Child class:

Children. h

/// Children. h // 12_DesignStyle /// Created by jiangwei on 14-10-11. // Copyright (c) 2014 jiangwei. all rights reserved. // # import <Foundation/Foundation. h> @ class Children; // if this line of code is not available, the Children type in the ChildrenDelegate protocol cannot be found. The error @ protocol ChildrenDelegate <NSObject> @ required-(void) wash :( Children *) children;-(void) play :( Children *) children; @ end @ interface Children: NSObject {// Nure * _ nure; // nanny // polymorphism technology can be used here, because nannies and nurses share the same parent class NSObject, but this method is not used here, instead, the id type // is used, but we also need to add some methods for this type. Here we use the Protocol // This proxy object must comply with the ChildrenDelegate protocol id <ChildrenDelegate> _ delegate; // This variable is the child's proxy object NSInteger timeValue;}-(void) setDelegate :( id) delegate; @ end
Here we define a Protocol: ChildrenDelegate, which has two necessary methods: wash and play

We also defined a very important attribute.

_ Delegate

This attribute definition is a bit different. This is the essence of implementing the proxy object. id is of an uncertain type, so the _ delegate variable can be assigned the following type:

You only need to implement the ChildrenDelegate protocol class. Remember here, this definition method will be used in the future. Equivalent to the interface type in Java, only the type of its implementation class can be assigned. The definition format is as follows:Id <protocol name>

Then there is a method for setting the Protocol. Note that the parameter type must also be id.

In fact, this also involves the mentioned polymorphism features, so the proxy mode is also based on the polymorphism features.


Children. m

/// Children. m // 12_DesignStyle /// Created by jiangwei on 14-10-11. // Copyright (c) 2014 jiangwei. all rights reserved. // # import "Children. h "// some actions of the nanny are used here. // if you want to hire a nurse now, we have to ask a nurse again. The code here needs to be changed, the reason for changing the nanny location to the nurse location is that the coupling degree is too high and the nanny and the child are coupled. If you need to change the location, you need to change the code // @ implementation Children-(id) init {self = [super init]; if (self! = Nil) {[NSTimer scheduledTimerWithTimeInterval: 1 target: self selector: @ selector (timerAction :) userInfo: nil repeats: YES];} return self;}-(void) setDelegate :( id) delegate {_ delegate = delegate;}-(void) timerAction :( NSTimer *) timer {timeValue ++; if (timeValue = 5) {[_ delegate wash: self];} if (timeValue = 10) {[_ delegate play: self] ;}}@ end
We have customized an initialization method and made a timer in the initialization method.

[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
In OC, a simple timer is Enabled: The timerAction method in self is executed every 1 s.

The timer in OC is different from that in java. Its execution logic can be implemented independently in a specified method (the function pointer in C is similar, as long as a function pointer is passed, you can execute the specified function, so @ selector does this), but Java must implement the Runable interface to execute the specified logic code in the run method.

In the timerAction method, when the time reaches 5s, we will execute the wash Method of the proxy object and the play method in 10 s.


Let's take a look at the nurses:

Nure. h

//// Nure. h // 12_DesignStyle /// Created by jiangwei on 14-10-11. // Copyright (c) 2014 jiangwei. all rights reserved. // # import <Foundation/Foundation. h> # import "Children. h "@ interface Nure: NSObject <ChildrenDelegate>-(void) wash :( Children *) children;-(void) play :( Children *) children; @ end
Nurses are simple and implement the ChildrenDelegate protocol.


Nure. m

//// Nure. m // 12_DesignStyle /// Created by jiangwei on 14-10-11. // Copyright (c) 2014 jiangwei. all rights reserved. // # import "Nure. h "# import" Children. h "@ implementation Nure-(void) wash :( Children *) children {NSLog (@" the child is dirty, and the nanny takes a bath for the child ");}-(void) play :( Children *) children {NSLog (@ "Children cry, play with children");} @ end
Here we will implement the wash and play methods.


Let's take a look at the nanny class:

Nanny. h

/// Nanny. h // 12_DesignStyle /// Created by jiangwei on 14-10-11. // Copyright (c) 2014 jiangwei. all rights reserved. // # import <Foundation/Foundation. h> # import "Children. h "@ interface Nanny: NSObject <ChildrenDelegate>-(void) wash :( Children *) children;-(void) play :( Children *) children; @ end

Nanny. m

/// Nanny. m // 12_DesignStyle /// Created by jiangwei on 14-10-11. // Copyright (c) 2014 jiangwei. all rights reserved. // # import "Nanny. h "# import" Children. h "@ implementation Nanny-(void) wash :( Children *) children {NSLog (@" the child is dirty, and the nurse takes a bath for the child ");}-(void) play :( Children *) children {NSLog (@ "Children cry, nurses play with children");} @ end
The Code logic of the nanny and nurse classes is the same, because both of them implement a protocol.


Test class

Main. m

//// Main. m // 12_DesignStyle /// Created by jiangwei on 14-10-11. // Copyright (c) 2014 jiangwei. all rights reserved. // # import <Foundation/Foundation. h> # import "Children. h "# import" Nure. h "# import" Nanny. h "// core: id type + protocol // perform low coupling operations // At the same time, int main (int argc, const char * argv []) can be used for communication between two classes. {@ autoreleasepool {Children * child = [[Children alloc] init]; Nure * nure = [[Nure alloc] init]; Nanny * nanny = [[Nanny alloc] init]; [child setDelegate: nanny]; // [child setNure: nure]; [[nsunloop currentRunLoop] run];} return 0 ;}
As you can see, the test class is very simple. We also discovered that the benefits of the proxy mode are also apparent. For example, if another person comes to take care of the child: Mom class, we just need to let the mom class implement that protocol. This coupling degree is not very high. Therefore, the proxy mode is extremely useful. When we develop IOS later, we will find that many proxy modes are used in it.


Running result:




Summary

This article describes how to implement the proxy mode in OC. In fact, the core technology of the proxy mode in OC is:Id type + protocol + Polymorphism




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.