Objective cにはデリゲートパターンがあります
VariableChanger.h
@protocol VariableChangedProtocol
-(void) variableDidChange;
@end
@interface VariableChangerClass : NSObject
{
id<VariableChangedProtocol> delegate;
int exampleVariable;
}
@property int exampleVariable;
-(id) initWithDelegate:(id<VariableChangedProtocol>) target;
@end
VariableChanger.m
@implementation
@synthesize exampleVariable;
-(id) initWithDelegate:(id<VariableChangedProtocol>) target
{
self = [super init];
if (self)
{
delegate = target;
}
}
-(void) setExampleVariable:(int) newValue
{
if(newValue != exampleVariable)
exampleVariable = newValue;
[delegate variableDidChange];
}
@end
VariableChangedListener.m
@implementation VariableChangedListener
-(id) init
{
self = [super init];
if (self)
{
VariableChangerClass *v = [[VariableChangerClass alloc] initWithDelegate:self];
}
return self;
}
-(void) variableDidChange
{
NSLog(@"The variable changed");
}
@end