IOS Singleton mode

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.