Design pattern ./--> is a means, a tool. Make code easier to write
1. Proxy design mode (delegate design mode)
<1>. The occasion of the listener
Object B (Nanny) want to listen to some behavior of object A (baby)
<2>. Notification of the occasion
Object A (Baby) Some behavior has occurred, want to inform B (Nanny) [B (Nanny) as a proxy object]
<3>. Some things, do not want to deal with their own, can give others to deal with
Object baby something happened and don't want to deal with it by hand to the nanny
2. Application of proxy design mode
When object A has some behavior, want to tell the object b-->[to make object b a proxy object]
Object B wants to listen to some of the behavior of object a-->[make object b The proxy object of object A]
When object A cannot handle certain behaviors, want object B to help handle-->[to make object b the proxy object of object A]
2. Single Case design mode
You can guarantee that the object created by a class has only one
The effect is: save memory overhead
If there is some data, the entire program is used, only the same resource is needed. (Ensure that everyone's access to the data is the same, is consistent)
Generally speaking, the tool class is designed as a singleton mode.
Realize:
Non-arc mode:
Static Gmtool * _gmtool = nil;
+ (Instancetype) Allocwithzone: (struct _nszone *) zone
{
if (_gmtool = = nil) {
Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{_gmtool = [super Allocwithzone:zone;];});
}
return _gmtool;
}
-(OneWay void) release{}//Guaranteed object not to be destroyed
-(ID) retain{return self;}
-(Nsinteger) retaincount{return 1;}
-(ID) init
{if (_gmtool = = nil)
Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{_gmtool = [Super init]; return _gmtool;});
}
+ (Instancetype) Sharegmtool
{
return [[Self alloc]init];
}
Arc Mode:-fobjc-arc
Static Gmtool * _gmtool = nil;
+ (Instancetype) Allocwithzone: (struct _nszone *) zone
{
if (_gmtool = = nil) {
Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{_gmtool = [super Allocwithzone:zone;];});
}
return _gmtool;
}
-(ID) init
{if (_gmtool = = nil)
Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{_gmtool = [Super init]; return _gmtool;});
}
+ (Instancetype) Sharegmtool
{
return [[Self alloc]init];
}
UIApplication shareapplication
Nsuserdefaults Standard
Uidevice Currentdevice
Nsfilemanager Defaultmanager
IOS Singleton mode