OC中代理服务器的使用的如何使用

[Objective-C]OC中代理(委托)设计模式
代理设计模式的基本概念
代理是指一个对象提供机会会对另一个对象中行为发生变化时做出的反应。
总而言之,代理设计默认的基本思想----两个对象协同解决问题,通常运用于对象间通信。
代理设计模式的基本特点
简化了对象的行为,最大化减小对象之间的耦合度
使用代理,一般来说无需子类化
简化了我们应用程序的开发,既容易实现,而且灵活
下面我们使用租房子的一个小例子来模拟代理模式
(在其中:房子的主人为:真实角色RealSubject;中介为:代理角色P作为要去租房子的我们,
我们平时一般见不到房子的主人,此时我们去找中介,让中介直接和找房主交涉,把房子租下来,
直接跟租房子打交道的是中介。中介就是在租房子的过程中的代理者),看下结构图:
1:协议声明:
@protocol FindApartment
-(void)findA
2:代理角色声明(Agent.h)头文件声明
#import &FindApartment.h&
@interface Agent : NSObject
3:代理角色实现(Agent.m)实现文件
#import &Agent.h&
@implementation Agent
-(void)findApartment{
NSLog(@&findApartment&);
4:真实角色声明(Person.h)头文件声明
#import &FindApartment.h&
@interface Person : NSObject
NSString *_
id _ //委托人(具备中介找房子的协议)
@property(nonatomic,copy) NSString *
@property(nonatomic,assign)id
-(id)initWithName:(NSString *)name withDelegate:(id)
-(void)wantToFindA
5:真实角色实现(Person.m)实现
#import &Person.h&
//定义私有方法
@interface Person()
-(void)startFindApartment:(NSTimer *)
@implementation Person
@synthesize name=_
@synthesize delegate=_
-(id)initWithName:(NSString *)name withDelegate:(id) delegate{
self=[super init];
self.name=
self.delegate=
-(void)wantToFindApartment{
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(startFindApartment:) userInfo:@&Hello& repeats:YES];
-(void)startFindApartment:(NSTimer *)timer{
//NSString *userInfo=[timer userInfo];
[self.delegate findApartment];
6:测试代理main.m方法
#import &Person.h&
#import &Agent.h&
int main(int argc, const char * argv[])
@autoreleasepool {
// insert code here...
NSLog(@&Hello, World!&);
Agent *agent=[[Agent alloc]init];
Person *jack=[[Person alloc]initWithName:@&jack& withDelegate:agent];
[jack wantToFindApartment];
[[NSRunLoop currentRunLoop]run];
[jack autorelease];扫一扫体验手机阅读
iOS开发篇——OC 协议和代理设计模式介绍
<span type="1" blog_id="1327907" userid='
分享到朋友圈
关注作者,不错过每一篇精彩没有更多推荐了,
不良信息举报
举报内容:
ios中关于delegate(委托)的使用心得
举报原因:
原文地址:
原因补充:
最多只允许输入30个字
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!Access denied | www.bkjia.com used Cloudflare to restrict access
Please enable cookies.
What happened?
The owner of this website (www.bkjia.com) has banned your access based on your browser's signature (420f3c7f210f6c0a-ua98).OC_经典笔记――delegate(代理~)_百度文库
赠送免券下载特权
10W篇文档免费专享
部分付费文档8折起
每天抽奖多种福利
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
OC_经典笔记――delegate(代理~)
&&OC delegete 代理
阅读已结束,下载本文需要
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,同时保存到云知识,更方便管理
&#xe64e;加入VIP
还剩1页未读,
定制HR最喜欢的简历
你可能喜欢

我要回帖

更多关于 代理模式的使用场景 的文章

 

随机推荐