Objective-C: About Delegates

I will be paraphrasing an extract from the excellent objective-C text, Cocoa Design Patterns  by Donald A. Yacktman, which is an excellent book on design patterns for for effective framework usage. In this extract, we will talk about Delegates

delegate is a reactionary object influenced by the behaviour of another object, “the basic idea that two objects could solve a problem” , sending messages from the one object to the other object acting as a delegate, during critical moments.  Whilst one can subclass in order to create new behaviours, this intends for the objects to become tightly coupled.
A further explanation of delegates
Delegates as an object are referenced using the typing of id


@interface MYBarView : NSView {
}
IBOutlet id delegate;
NSColor *barColor;
float barValue;

//! Accessors
-(id)delegate;
-(void)setDelegate:(id)anObject;
-(float)barValue;
-(void)setBarValue:(float)aValue;
-(NSColor *)barColor;
-(void)setBarColor:(NSColor *)aColor;

The following Objective-C 2.0 formal protocol defines the messages with the @optional key word so that delegates that conform to the protocol don’t have to implement all of the methods:


//! Formal protocol defines messages sent from MYBarView to its
// delegate

@protocol MYBarViewDelegate

@optional

-(float)barView:(id)barView shouldChangeValue:(float)newValue;
-(void)barViewWillChangeValue:(NSNotification *)aNotification;

- (void)barViewDidChangeValue:(NSNotification *)aNotification;
@end

By declaring the messages that may be sent to the delegate, we use formal protocols with @optional term so that the delegates don’t have to conform to all the methods above. 


//! Informal protocol defines messages sent from MYBarView to its // delegate @interface NSObject (MYBarViewDelegateSupport)
-(float)barView:(id)barView shouldChangeValue:(float)newValue;
-(void)barViewWillChangeValue:(NSNotification *)aNotification;

- (void)barViewDidChangeValue:(NSNotification *)aNotification;
@end

The code above is an example of informal protocols

Implementing the delegate
So to implement the delegate object we have declared, we simply implement the methods that correspond to whichever delegate methods we want to receive, depending on what we had as optional and/or mandatory. 


@implementation MYValueLimitColorChanger

//! Delegate messages
- (float)barView:(id)barView shouldChangeValue:(float)newValue {
float result = newValue;
if (0.25f > result)
{
result = 0.25f;
}
return result;
}

- (void)barViewDidChangeValue:(NSNotification *)aNotification {
if(0.75f < [[aNotification object] barValue]) {
[[aNotification object] setBarColor:[NSColor blackColor]];
} else {
[[aNotification object] setBarColor:[NSColor grayColor]];
}
}

@end

This was just an extract from the chapter on delegates, from the book Cocoa Design Patterns by Donald A. Yacktman (2009).  The book is at 40% off and with free shipping, definitely worth checking out.

 

Advertisements
This entry was posted in General and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s