Reading Notes-examples of common design patterns and Reading Notes

Source: Internet
Author: User

Reading Notes-examples of common design patterns and Reading Notes

1. The Singleton mode is used to solve the "only one instance in the application" problem.

2. In the life cycle of an iOS application, sometimes we only need one instance of a class. If there are multiple instances, it will occupy too much memory. For example, when a program starts, the application state is maintained by an instance of the UIApplication class. This instance represents the entire "Application Object" and can only be one instance, its role is to implement access to some shared resources in the program and protection of the status.

3. The Singleton mode encapsulates a static attribute and provides a method for creating a static instance. The Code is as follows:

1 // 2 // Singleton. h 3 // 4 5 @ interface Singleton: NSObject 6 7 + (Singleton *) sharedManager; 8 // attribute 9 @ property (nonatomic, strong) NSString * singletonData; 10 11 @ end12 13 --------------------------------------------------------------------------- 14 15 // 16 // Singleton. m17 // 18 19 # import "Singleton. h "20 21 @ implementation Singleton22 static Singleton * sharedManager = nil; 23 // method for creating a static instance 24 + (Singleton *) sharedManager25 {26 static dispatch_once_t once; 27 dispatch_once (& once, ^ {28 sharedManager = [[self alloc] init]; 29}); 30 return sharedManager; 31} 32 // dispatch_once (dispatch_once_t * predicate, dispatch_block_t block); 33 blocks @ end

The sharedManager method uses the dispatch_once function (GCD technology, Grand Central Dispatch, is a C-language-based multi-threaded access technology). It receives a dispatch_once_t parameter, the struct provided by The GCD parameter, send the GCD address to the dispatch_once function, which records whether the code block has been called. There is also a block parameter. For a given predicate, this function ensures that the relevant blocks will be executed only once. The most important thing is that this method is thread-safe, you do not need statements such as thread lock @ synchronized. Note that for blocks that only need to be executed once, the input predicate must be identical, so predicate is often modified using static or global. In this way, code writing is simple and clear, and thread security is achieved, so the implementation efficiency is very high.

4. In the Cocoa Touch framework, there are tickets such as UIApplication, UIaccelerometer, NSUserDefaults, and nsicationicationcenter. In addition, although the NSFileManager and NSBundle classes belong to the Cocoa framework, they can also be used in the Cocoa Touch framework. (The Singleton classes in the Cocoa framework include NSFileManager, NSBundle, NSWorkspace, and NSApplication ).

4.1. UIApplication

An instance of the UIApplication class provides a centralized control point for the application to maintain the application state. The UIApplication instance is always assigned to the application delegate object (UIApplicationDelegate) to respond to low memory, application startup, background running, and application termination times through the Application delegate object. The following describes several methods and attributes of this class.

4.1.1 + sharedApplication method. Create and obtain a UIApplication instance.

4.1.2 idleTimerDisabled attribute. Set and obtain the "idle time prohibited" status. The default value is NO, that is, the screen is locked by default.

4.1.3-openURL: method. You can open some built-in iOS applications, including opening a browser, opening a map, making calls, sending text messages, and sending E-mail.

1 // open the browser 2 NSURL * url = [NSURL URLWithString: @ "http://www.baidu.com"]; 3 [[UIApplication sharedApplication] openURL: url]; 4 5 // when opening a Google Map, it is actually opened through a built-in browser. 6 NSString * searchQuery = @ "Xi'an, China

Related Article

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.