IOS Singleton mode (design mode 1)

Source: Internet
Author: User

IOS Singleton mode (design mode 1)

The Singleton mode is a common software design mode. Its core structure only contains a special class called Singleton class. The Singleton mode ensures that there is only one instance in a class in the system and the instance is easy to access, so as to conveniently control the number of instances and save system resources. If you want to have only one class object in the system, the singleton mode is the best solution.

Main advantages:

Provides controlled access to the unique instance. Because only one object exists in the system memory, system resources can be saved. The single-instance mode of some objects that require frequent creation and destruction can undoubtedly improve the system performance. Allows variable target instances.

Main disadvantages:

Because there is no abstraction layer in the single-profit mode, it is very difficult to expand the single-instance class. The responsibilities of the singleton class are too heavy, which violates the "single Responsibility Principle" to a certain extent ". Abuse of Singleton may cause some negative problems. For example, designing database connection pool objects as Singleton classes to save resources may lead to excessive programs sharing connection pool objects and connection pool overflow; if the instantiated object is not used for a long time, the system will regard it as garbage and be recycled, which will lead to the loss of the object state. Set the singleton mode for the HSCommonTool class
#import 
  
   @interface HSCommonTool : NSObject+ (instancetype)sharedCommonTool;@end
  
# Import HSCommonTool. h @ interface HSCommonTool ()
  
   
@ End @ implementation HSCommonTool // defines a static variable static HSCommonTool * _ commonTool; // rewrite the allocWithZone method. The alloc Internal call method + (instancetype) allocWithZone :( struct _ NSZone *) zone {// set allocWithZone to execute static dispatch_once_t onceToken only once; dispatch_once (& onceToken, ^ {_ commonTool = [super allocWithZone: zone] ;}); return _ commonTool ;} // call this method when copying an object-(id) copyWithZone :( nullable NSZone *) zone {return _ commonTool;} // write a class method to facilitate external calls + (instancetype) sharedCommonTool {static dispatch_once_t onceToken; dispatch_once (& onceToken, ^ {_ commonTool = [[self alloc] init] ;}); return _ commonTool;} @ end
  
Encapsulation of Singleton mode-HSSingleton. h
//. Hfile # define HSGSingletonH (name) + (instancetype) shared # name ;//. m file # define HSGSingletonM (name) static id _ instance; + (instancetype) allocWithZone :( struct _ NSZone *) zone {static dispatch_once_t onceToken; dispatch_once (& onceToken, ^ {_ instance = [super allocWithZone: zone] ;}); return _ instance ;}+ (instancetype) shared ## name {static dispatch_once_t onceToken; dispatch_once (& onceToken, ^ {_ instance = [[self alloc] init] ;}); return _ instance ;}- (id) copyWithZone :( NSZone *) zone {return _ instance ;}

 

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.