A single example of iOS design patterns

Source: Internet
Author: User

IOS Single case

1. What is a single case?

is a class that has only one instance object in the system. This instance object is accessed through one of the global portals.

2. Application Scenarios?

Used to expect only one instance of a class, without running a class with more than two instances.

The A:ios SDK is applied to the Singleton mode class:

Shareapplication Unified Management of UIApplication

Nsuserdefaults's Standarduserdefaults Unified management user profile,

Nsfilemanager's Defaultmanager is responsible for the management of physical documents,

Defaultcenter is responsible for global message distribution in Nsnoficationcenter.

B: Custom Scene: Theme management, download management, transfer data;

3. How to implement and use?

ARC + GCD Implementation

Xx.h

1 + (Arcsingleton *) sharedinstance;

Xx.m

1+ (Arcsingleton *) Sharedinstance2 {3     //14     StaticArcsingleton *sharedinstance =Nil;5     //26     Staticdispatch_once_t Oncetoken;//Lock7     //38Dispatch_once (&oncetoken, ^ {//call at most once9Sharedinstance =[[Self alloc] init];Ten     }); One     returnsharedinstance; A } -  - //This init method is called when the Singleton is used for the first time.  the- (ID) Init - { -Self =[Super init]; -  +     if(self) { -         //usually do some related initialization tasks here +     } A  at     returnSelf ; -}


1. Declaring a static variable to save an instance of the class, ensuring its global availability in the class

·2. Declares a static variable dispatch_once_t, which ensures that the initializer code executes only once

·3. Use the Grand Central Dispatch (GCD) to perform the initialization of the block. This is the key to the singleton pattern: Once the class has been initialized, the initializer will never be called again.

4. Single-Instance Factory

Managing a large number of individual cases in a project

The essence of the factory method pattern is "to define an interface to create an object, but let the class implementing the interface decide which class to instantiate." The factory method defers instantiation of a class to a subclass. ”

5. Module Management System

Unified Management

---------------------------------------------

Reference

The evolutionary http://blog.jobbole.com/56439/of the single case model of 1.iOS design pattern reflection

2.iOS design Mode (02): Single Case mode http://beyondvincent.com/blog/2013/05/09/20/

A single example of iOS design patterns

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.