UI学习 第九章 委托(代理)设计模式 单例设计模式
来源:互联网 发布:人工智能创造新就业 编辑:程序博客网 时间:2024/05/21 08:29
UI学习 第九章 委托(代理)设计模式 单例设计模式
委托(代理)设计模式
一个人找中介买房子为例
main.m
#import<Foundation/Foundation.h>
#import "Buyer.h"
intmain(intargc, const char * argv[]) {
@autoreleasepool {
//假设有一个人需要房子,这个人找到中介就行
Buyer *buyer = [[Buyeralloc]init];
[buyer findMediation];
}
return 0;
#import "Buyer.h"
intmain(intargc, const char * argv[]) {
@autoreleasepool {
//假设有一个人需要房子,这个人找到中介就行
Buyer *buyer = [[Buyeralloc]init];
[buyer findMediation];
}
return 0;
}
Buyer.h
#import<Foundation/Foundation.h>
#import "Mediation.h"
@interfaceBuyer : NSObject<PaymentDelegate>//要找房子必须要签协议
-(void)findMediation;//我要找一个中介
#import "Mediation.h"
@interfaceBuyer : NSObject<PaymentDelegate>//要找房子必须要签协议
-(void)findMediation;//我要找一个中介
@end
Buyer.m
#import"Buyer.h"
@implementationBuyer
-(void)findMediation{//去找中介,让中介帮我找房源
NSLog(@"我需要房源");
Mediation *m = [[Mediationalloc]init];//找到中介了
m.delegate= self;//跟中介签协议的是我,不能忘记写
[m handle];//中介帮我找房子了
}
-(void)payment:(float)money{//中介让我付钱
NSLog(@"付钱:%.2f",money);
}
@implementationBuyer
-(void)findMediation{//去找中介,让中介帮我找房源
NSLog(@"我需要房源");
Mediation *m = [[Mediationalloc]init];//找到中介了
m.delegate= self;//跟中介签协议的是我,不能忘记写
[m handle];//中介帮我找房子了
}
-(void)payment:(float)money{//中介让我付钱
NSLog(@"付钱:%.2f",money);
}
@end
Meidition.h
//在被委托者的声明文件(.h)中进行协议的声明
#import<Foundation/Foundation.h>
@protocolPaymentDelegate <NSObject>
-(void)payment:(float)money;
@end//这是一个协议,在被委托者的声明文件(.h)中进行协议的声明
@interfaceMediation : NSObject
@property(nonatomic,weak)id<PaymentDelegate> delegate;//是谁给我签了协议
-(void)handle;//我得有找做一些事情的能力
#import<Foundation/Foundation.h>
@protocolPaymentDelegate <NSObject>
-(void)payment:(float)money;
@end//这是一个协议,在被委托者的声明文件(.h)中进行协议的声明
@interfaceMediation : NSObject
@property(nonatomic,weak)id<PaymentDelegate> delegate;//是谁给我签了协议
-(void)handle;//我得有找做一些事情的能力
@end
Meidition.m
#import"Mediation.h"
@implementationMediation
-(void)handle{//找到房源了
NSLog(@"找到房源,请给钱");
[_delegatepayment:10.0];//我得问签协议的那个人要我的房钱
}
@implementationMediation
-(void)handle{//找到房源了
NSLog(@"找到房源,请给钱");
[_delegatepayment:10.0];//我得问签协议的那个人要我的房钱
}
@end
委托主要作用:
1.回传值
2.当我们声明一个遵循了协议的属性时,属性的关键字要用,weak,assing,目的是为了避免循环引用,
3.委托最大的特点一对一。
4.委托一般是具有上下级关系的两个类
委托怎么用??
第一步:首先在被委托的那个类的.m文件里声明一个协议(也可单独建一个文件,不过不常用),协议中的方法是要做的事情。
第二步:让委托者遵守这个协议。
第三部:让被委托者做事情。很多时候需要在被委托者类里进行赋值并且回传,如:
[_delegateaddCell:name.text:phone.text];
[self.navigationControllerpopViewControllerAnimated:YES];
第四部:让委托者实现这个协议方法。
单例设计模式
如果不使用单例,每次alloc init 都会产生一个新的对象,如果使用单例,重写初始化方法,那么每次初始化的对象都是同一个对象。
#import"Student.h"
staticStudent*stu = nil;
@implementationStudent
+(Student*)shareStudent{
@synchronized(self){//避免多线程操作带来的弊端
if (stu== nil) {
stu = [[Studentalloc]init];
}
return stu;
}
}
//避免alloc产生新对象 重写alloc方法
+(instancetype)allocWithZone:(struct_NSZone*)zone{
if (stu== nil) {
stu = [superallocWithZone:zone];
}
return stu;
}
//避免copy新对象,重写copy方法
-(id)copyWithZone:(NSZone*)zone{
return stu;
}
-(id)mutableCopyWithZone:(NSZone*)zone{
return stu;
}
staticStudent*stu = nil;
@implementationStudent
+(Student*)shareStudent{
@synchronized(self){//避免多线程操作带来的弊端
if (stu== nil) {
stu = [[Studentalloc]init];
}
return stu;
}
}
//避免alloc产生新对象 重写alloc方法
+(instancetype)allocWithZone:(struct_NSZone*)zone{
if (stu== nil) {
stu = [superallocWithZone:zone];
}
return stu;
}
//避免copy新对象,重写copy方法
-(id)copyWithZone:(NSZone*)zone{
return stu;
}
-(id)mutableCopyWithZone:(NSZone*)zone{
return stu;
}
@end
0 0
- UI学习 第九章 委托(代理)设计模式 单例设计模式
- 委托(代理) 设计模式
- 委托设计模式(即代理模式)
- 代理设计模式、工厂设计模式、单例设计模式
- 设计模式(单例、代理、工厂)
- OC设计模式之代理(委托)设计模式
- 委托代理设计模式(协议)
- 设计模式之委托代理(delegate)
- delegate代理(委托)设计模式
- 浅谈代理/委托设计模式
- Swift 委托/代理设计模式
- iOS 代理委托设计模式
- 设计模式学习(单例模式)
- 设计模式-代理,KVO,单例设计模式
- UI 04 代理设计模式
- 单例设计模式的UI框架
- 设计模式之单例、策略模式、代理模式
- 单例设计模式学习
- 文件的输入输出笔记
- 大数据学习的资料
- Android Looper详解
- iPhone开发【二十七】数据持久化总结之第6篇(终结篇)—5种数据持久化方法对比总结
- RecyclerView实现Gallery
- UI学习 第九章 委托(代理)设计模式 单例设计模式
- android RecyclerView学习笔记(二)上拉刷新下拉加载
- 大家来学javaBean
- stringstream 的简单用法
- weblogic设置启动不需要密码
- C++函数指针数组的使用方法
- UI学习 第十章 KVC KVO 通知
- HDOJ 1285 确定比赛名次 (拓扑排序)
- 黑马程序员——OC语言——类和对象