- 单例模式:通过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是典型的通知模式,观察某个属性的状态,状态发生变化时通知观察者。
- 委托模式:代理+协议的组合。实现1对1的反相传值操作。
- 工厂模式:通过一个类方法,批量的根据已有模板生产对象。
- MVC模式:Model View Control, 把模型 视图 控制器 层进行解耦合编写。
- MVVM模式:Model View ViewModel 把 模型 视图 业务逻辑 层进行解耦合编写。