Common IOS design modes-singleton mode (IOS development)

Source: Internet
Author: User

Common IOS design modes-singleton mode (IOS development)

Common IOS design modes include Singleton mode, delegate mode, observer mode, and MVC mode.

The Singleton mode is described here.


Singleton Mode

-Problem: only one instance is used in the application (only instances of a class are required)

-Principle: A static attribute is encapsulated and a method for creating a static instance is provided.

-Application: Singleton type

// Singleton. h @ interface Singleton: NSObject + (Singleton *) sharedManager; @ property (nonatomic, strong) NSString * stingletonData; @ end

# Import "Singleton. h "@ implementation Singleton @ synthesize SingletonData = _ singletonData; static Singleton * sharedManager = nil; + (Singleton *) sharedManager {static exactly once; static dispatch_once_t onceToken; dispatch_once (& once, ^ {shardManager = [[self alloc] init] ;}); return shardManager ;}@ end

Contains a static variable singletonData. The class method adopts the GCD Technology (based on the multi-thread access technology of C language ), it only executes a code block ({}) once throughout the application lifecycle ({}). Dispatch_once_t is The GCD struct. A pointer must be passed during use.

The dispatch_once function ensures that the function is only run once, and also means that the running is thread-synchronized.




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.