协议:TellREcevie.h 与 SendProtocol.h
TellREcevie.h
-(void)tellRecevie:(NSNumber *)money;
SendProtocol.h
-(void)sendMoney:(NSNumber *)money;
流程,userA转钱给银行,银行转钱给B,B收到钱了通知银行,银行通知A
#import <Foundation/Foundation.h>
#import "UserA.h"
#import "UserB.h"
#import "Bank.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Bank * bank = [[[Bank alloc] init] autorelease];
UserA * a =[[[UserA alloc] init] autorelease];
UserB * b = [[[UserB alloc] init] autorelease];
a.delegate = bank;
[a giveMoney:100];
bank.delegate = b;
[bank giveMoney];
b.delegate = bank;
[b respondtoBank];
bank.delegate1 = a;
[bank tellUserA];
}
return 0;
}
UserA.h
#import <Foundation/Foundation.h>
#import "SendProtocol.h"
#import "TellRecevie.h"
@interface UserA : NSObject<TellRecevie>
{
int _count;
id<SendProtocol>_delegate;
}
@property (nonatomic,assign) int count;
@property (nonatomic,assign) id<SendProtocol>delegate;
-(void)giveMoney:(int) money;
@end
- (id)init
{
self = [super init];
if (self) {
_count=1000;
NSLog(@"%@:当前账户余额总额为:%d",[self class],_count);
}
return self;
}
@synthesize count = _count;
@synthesize delegate = _delegate;
#pragma mark - A to Bank
-(void)giveMoney:(int)money
{
//c类型必须转换成NSNumber
if (_delegate && [_delegate respondsToSelector:@selector(sendMoney:)]) {
NSLog(@"%@:开始转账,金额为%d",[self class],money);
[_delegate performSelector:@selector(sendMoney:) withObject: [NSNumber numberWithInt:money]];
}
}
#pragma mark - Bank tell A
-(void)tellRecevie:(NSNumber *)money
{
_count -= money.intValue;
NSLog(@"%@:寄款人收到转账,当前余额为:%d",[self class],_count );
}
#import <Foundation/Foundation.h>
#import "SendProtocol.h"
#import "TellRecevie.h"
@interface Bank : NSObject<SendProtocol,TellRecevie>
{
id<SendProtocol> _delegate;
id<TellRecevie> _delegate1;
}
@property(nonatomic,assign) id<SendProtocol>delegate;
@property(nonatomic,assign) id<TellRecevie>delegate1;
-(void)giveMoney;
-(void)tellUserA;
@end
Bank.m
{
NSNumber * temp;
}
@synthesize delegate = _delegate;
@synthesize delegate1 = _delegate1;
#pragma mark - A to Bank
-(void)sendMoney:(NSNumber *)money
{
NSLog(@"%@:receive %@",[self class],money);
temp = money;
}
#pragma mark - Bank to B
-(void)giveMoney
{
if (_delegate && [_delegate respondsToSelector:@selector(sendMoney:)]) {
[_delegate performSelector:@selector(sendMoney:) withObject:temp];
}
}
#pragma mark - B tell Bank
-(void)tellRecevie:(NSNumber *)money
{
temp = money;
NSLog(@"%@:receive 收款人收到金额的信息,收款人收到的金额为:%@",[self class],temp);
}
#pragma mark - Bank tell A
-(void)tellUserA
{
if (_delegate1 && [_delegate1 respondsToSelector:@selector(tellRecevie:)]) {
NSLog(@"%@:告诉寄款人收款人已经收到金额%@",[self class],temp);
[_delegate1 performSelector:@selector(tellRecevie:) withObject:temp];
}
}
UserB.h
#import <Foundation/Foundation.h>
#import "SendProtocol.h"
#import "TellRecevie.h"
@interface UserB : NSObject<SendProtocol>
{
int _count;
id <TellRecevie>_delegate;
NSNumber *_recevie;
}
@property (nonatomic,assign) id<TellRecevie>delegate;
@property (nonatomic,assign) int count;
@property (nonatomic,assign) NSNumber *recvive;
-(void)respondtoBank;
@end
UserB.m
@implementation UserB
@synthesize delegate = _delegate;
@synthesize count = _count;
@synthesize recvive = _recevie;
- (id)init
{
self = [super init];
if (self) {
_count=1000;
NSLog(@"%@:当前余额总额为:%d",[self class],_count);
}
return self;
}
#pragma mark - A to Bank
-(void)sendMoney:(NSNumber *)money
{
_count = money.intValue;
NSLog(@"%@:收到%@元,当前余额账户为%d",[self class],money,_count );
_recevie = money;
}
#pragma mark = TellRecevie
-(void)respondtoBank
{
if (_delegate &&[_delegate respondsToSelector:@selector(tellRecevie:)]) {
NSLog(@"%@:告诉银行,已经收到金额%@",[self class],_recevie);
[_delegate performSelector:@selector(tellRecevie:) withObject:_recevie];
}
}