iphone understand delegation

来源:互联网 发布:nba十大数据 编辑:程序博客网 时间:2024/06/03 16:34

 

Overview

In this technical document I will discuss how to implement one of the most important designs patterns: the delegation pattern. All the examples are in Objective-C due this article is focused on iPhone development. This manual should be very useful for those who are developing for iPhone and want to improve the usability, modularity and extendability of their software projects.

 

The Basics

To take advantage of this document, you should be familiar whit Objective-C programming language, the iPhone development process and pattern designs.

read this Objective-C guide for an introduction to the Objective-C language.

read this iPhone development guide to understand how the iPhone development process works.

 

 

The delegation pattern (general overview)

In software engineering the delegation pattern is a design pattern in object-oriented programing where an object instead of performing one of its stated tasks, delegates that task to an associated helper object. The helper object is called the delegate.

In the delegation pattern we always have two main objects: the "delegator" and the "delegate" . An implementation example of the delegation pattern in the iPhone is the instance of the "UIAccelerometer" class. "UIAccelerometer" is the class who represents the physical accelerometer that all the iPhones have inside. This class delegates the function "accelerometer" so when a user move the phone, the UIAccelerometer sends "accelerometer" to its delegate. Then the "delegate" object decides what to do with the acceleration data.


 

 

 

In the example AccelerometerGraph the delegator is the class UIAccelerometerDelegate and the delegate is MainViewController.m:


The Delegator: declare the protocol

UIAccelerometerDelegate.h

@protocol UIAccelerometerDelegate

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration*)acceleration;

@end

 

border

 

The Delegate: Implement the protocol

MainViewController.h

@interface MainViewController : UIViewController<UIAccelerometerDelegate>

{

 

/* use the information contained in the acceleration object (x,y and z components of the acceleration)

to understand the movement of the user. */

 

/*code*/

}

 

MainViewController.m

// UIAccelerometerDelegate method, called when the device accelerates.

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration*)acceleration

{

/*code*/

}

 

 

 

The use of this pattern here it's very powerful because "apple" is programing parts of the process but they are delegating to the programmer specific parts like "how to react when a user rotate/move/shake the phone". The programer does not know how the accelerometer chip works and the only thing he has to do, to use the accelerometer of the iPhone, is to implement the UIAccelerometerDelegate protocol.

 

 

The delegation pattern (specific example)

In this section we will discuss in detail a full example of the delegation pattern. The example is about a robot. We are a robot programmers and we want to program a "line follower". A "line follower" is a robot that can follow a white line in a black surface. Our robot has two line sensors, one touch sensor and two motors. Click here to see the entire project.

 

 

 

As a programmers we decide to implement the general behavior of the robot (follow the line) and delegate the specific task: what to do when the robot find a wall.

 

 

 

To implement a delegation pattern we have to follow the next steps:

 

1) Delegating(the delegator):

1.1) Delclare a protocol.

1.2) Declare a delegate object.

1.3) Create a delegate property.

1.4) Declare the delegate constructor.

1.5) Declare the setDelegate function

1.6) Calling the protocol.


robot.h

// 1.1 -> Declare a protocol

@protocol robotDelegate

-(void)wallInFrontOfTheRobot;

@end

 

@interface robot : NSObject {

id <robotDelegatedelegate//1.2. -> declare a delegate object

}

-(bool) getTouchSensor; //return True is the robot is detecting a wall in front of him.

-(bool) getSensorRight; //return True if the right sensor is on the black line.

-(bool) getSensorLeft; //return True if the left sensor is on the black line.

-(void) setMotorRight:(int) speed; //speed: [-100...100]

-(void) setMotorLeft:(int) speed; //speed: [-100...100]

-(void) followLine; //the algorithm who follow the line

 

// 1.3 -> Declare a delegate property

@property(assignid <robotDelegate> delegate;

 

@end

 

robot.m

@implementation robot

 

// 1.4 -> Declare the delegate constructor

- (id <robotDelegate>)delegate

{

return delegate;

}

 

// 1.5 -> Declare the setDelegate method

- (void)setDelegate:(id <robotDelegate>)v

{

delegate = v;

}

 

//the algorithm who follow the line

-(void) followLine{

if([self getSensorLeft]){

//turn left

[self setMotorRight:100];

[self setMotorLeft:25];

}

else if([self getSensorRight]){

//turn right

[self setMotorRight:25];

[self setMotorLeft:100];

}else{

//go straight

[self setMotorRight:100];

[self setMotorLeft:100];

}

 

//delegating the process

if([self getTouchSensor]){

[delegate wallInFrontOfTheRobot]; //1.6 -> calling the protocol

}

}

 

 

 

 

2) Being a Delegate (the delegate)

2.1) Implement the protocol.

2.2) Set the delegate.


robotViewController.h

 

#import "robot.h"

@interface RobotViewController : UIViewController <robotDelegate> { //2.1 --> implement the protocol

robot *rob;

}

 

@end

 

robotViewController.m

 

// 2.1 --> implement the protocol

-(void)wallInFrontOfTheRobot{

NSLog(@"wallInFrontOfTheRobot");

[rob setMotorLeft:0];

[rob setMotorRight:0];

}

 

//function called every 0.1 seconds

-(void)main{

rob.delegate=self//2.2 --> set the delegate

[rob followLine];

}

 

 

 

In the example the programmer decide to stop the robot when it finds a wall. Another alternative would be make a turn of 180 degrees and try to find the line again.

To sum up, in this example we can see clearly the use of the delegation pattern. The robot programmer decides to program the general behavior of the robot (follow the line) and delegate the specific task of "what to do if the robot is in front of a wall". Also, we have seen what steps we should follow in order to implement the pattern. I encourage you to start to use this pattern in all your programs because will help you to write more reusable and extendible code.

 

Come back to technical notes

 

 

 

from:http://www.hivestudio.cat/index.php?option=com_content&view=article&id=57:technical-note-the-delegation-pattern-on-the-iphone&catid=35:technical-note-category&Itemid=76

 

 

 

 

理解:

代理就是某个实体器件对应的类,将某些处理方法空出,可以让具体实现该方法的对象进行具体的实现处理。例如实现了一个机器人,封装好一个机器人类,该类对机器人遇到障碍物如何处理不关心,仅仅留给委托者去实现处理,也即相应的机器人对象(具体的对象)去实现,

原创粉丝点击