- 单例模式:通过static关键词,声明全局变量。在整个进程运行期间只会被赋值一次。
通常我会在程序里.h里 1.声明一个类方法 + (GlobalClass*)sharedInstance;
2.全局变量://用户id
@property(nonatomic,copy)NSString*user_id;
.m
+ (GlobalClass*)sharedInstance
{
staticGlobalClass*sharedInstance;
@synchronized(self)
{
if(!sharedInstance)
{
sharedInstance = [[GlobalClassalloc]init];
}
returnsharedInstance;
}
}
程序里调用的时候 单利存值
NSdef= [NSUserDefaultsstandardUserDefaults];
if([NSdefobjectForKey:@"ID"]!=nil) {
[GlobalClasssharedInstance].user_id=[NSdefobjectForKey:@"ID"];
}
[NSdefsynchronize];
- 观察者模式:KVO是典型的通知模式,观察某个属性的状态,状态发生变化时通知观察者。
我是经常用在刷新页面,例如我从待支付进入到支付详情页,点击了支付,如果回调结果是success,我会给上一个页面发送一条通知,通知的方法是刷新页面。
发送通知:(在pop前写)
[[NSNotificationCenterdefaultCenter]postNotificationName:@"MEDICINE"object:nil];
接收通知页面:
//网络
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(loginsure)name:@"MEDICINE"object:nil];
执行loginsure 是对页面进行刷新。
- 委托模式:代理+协议的组合。实现1对1的反相传值操作。
- 工厂模式:通过一个类方法,批量的根据已有模板生产对象。
- MVC模式:Model View Control, 把模型 视图 控制器 层进行解耦合编写。
- MVVM模式:Model View ViewModel 把 模型 视图 业务逻辑 层进行解耦合编写。











网友评论