IOS Singleton mode (design mode one)

Source: Internet
Author: User

Singleton mode is a kind of common software design pattern. In its core structure, it contains only a special class called a singleton class. The singleton mode can ensure that there is only one instance of a class in the system, and the instance is easy to be accessed by the outside world, thus it is convenient to control the number of instances and save system resources. Singleton mode is the best solution if you want to have only one object for a class in the system.

    • Key Benefits:

      • Provides controlled access to a unique instance.
      • Because there is only one object in system memory, system resources can be saved, and for some object singleton patterns that need to be created and destroyed frequently, the performance of the system can be improved.
      • Allows a variable number of instances.
    • Main disadvantages:

      • Because there is no abstraction layer in the simple interest pattern, the expansion of the Singleton class is very difficult.
      • The responsibility of the Singleton class is too heavy, which violates the "single duty principle" to some extent.
      • The abuse of a single case will bring some negative problems, such as a singleton class designed to conserve resources for the database connection pool object, which could result in a connection pool overflow for programs that share connection pool objects, and if the instantiated objects are not exploited for a long time, the system is considered garbage and is recycled, which results in the loss of the object state.
给HSCommonTool类设定单例模式
#import <Foundation/Foundation.h>@interface HSCommonTool : NSObject+ (instancetype)sharedCommonTool;@end
#import "HSCommonTool.h"  @interface hscommontool() <nscopying>@end @implementation hscommontool //define a static variableStaticHscommontool *_commontool;//Override Allocwithzone method, Alloc internal call secondary method+ (Instancetype) Allocwithzone: (struct_nszone *) zone{//Set Allocwithzone to execute only once    Static dispatch_once_tOncetoken;dispatch_once(&oncetoken, ^{_commontool = [SuperAllocwithzone:zone]; });return_commontool;}//Copy object, call this method- (ID) Copywithzone: (Nullable Nszone *) zone{return_commontool;}//Write a class method to facilitate the outside call+ (Instancetype) sharedcommontool{Static dispatch_once_tOncetoken;dispatch_once(&oncetoken, ^{_commontool = [[ SelfALLOC] init]; });return_commontool;}@end
单例模式的封装 - HSSingleton.h
. h file#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) gkfx##Name{ Static dispatch_once_t Oncetoken;Dispatch_once (&Oncetoken, ^{ _instance =[[Self alloc]Init];    });return _instance;}  -(ID) Copywithzone: (Nszone *) zone{ return _instance;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

IOS Singleton mode (design mode one)

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.