Multithreading for iOS development-single-sample mode (ARC), iosarc

Source: Internet
Author: User

Multithreading for iOS development-single-sample mode (ARC), iosarc

Multithreading for iOS development-singleton mode (ARC)

1. Simple Description:

Design Mode: a set of experiences, methods, and tools summarized during software development over the years

There are 23 Design Patterns in java, and the singleton mode and proxy mode are the most common ones in ios.

Ii. Singleton Mode

(1) Singleton mode: This mode ensures that there is only one instance in a class during the program running, and the instance is easy for external access, so as to conveniently control the number of instances, and saves system resources.

(2) single-instance mode: Share a resource in the entire application (this resource only needs to be created and initialized once ), this class should always have only one object.

(3) The Singleton mode varies in the ARC \ MRC environment and requires two sets of different codes.

Macro can be used to determine whether it is an ARC environment



(4) Implementation of Singleton mode in ARC

Retain a Global static instance static id _ instance in. m;

1) override allocWithZone: method. Create a unique instance here (pay attention to thread security)



2) provides a class method to allow external access to a unique instance.



3) Implement copyWithZone: Method



(5) Non-ARC (MRC) implementation of the Singleton mode (several more steps than ARC)

Memory Management

-(Id) retain {return self ;}

-(NSUInteger) retainCount {return 1 ;}

-(Oneway void) release {}

-(Id) autorelease {return self ;}

Iii. Singleton mode (ARC)

1. Description

Override allocWithzone: Method to Control Memory allocation. Because alloc calls this method internally, the system creates a new memory space each time it calls allocWithzone: method.

In the alloc method: the memory is always allocated once.

In the init method: ensure that all MP3 data is loaded only once.

2. Sample Code

Creates an audio tool class that inherits the NSObject class.

Implement the following code in this class:



In the master controller, create a tool-Class Object:

 

Print result:

Note: The lower-layer method allocWithZone will be called inside alloc to allocate memory space. The above Code creates four different objects.

3. Singleton mode: Design Concept

(1) allocate only one piece of memory to create objects.

(2) provides a class method to return a unique internal variable

(3) it is best to ensure that the init method is only initialized once.

Sample Code:

Creates an audio tool class that inherits the NSObject class.

Implement the following code in this class according to the design philosophy:

YYAudioTool. m file




YYAudioTool. h file



Create an object in the master controller:

 

Observe the printed results:

Note: Only one object instance is created in the entire program.

4. static supplement:

Note: differences between static id instace = nil; and static id instace; instace = nil;

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.