iOS design mode-single case

Source: Internet
Author: User

iOS design mode-single case

Schematic diagram

Description

1. Singleton mode is used by everyone, strict singleton mode is seldom used

2. Strict singleton mode means that the object cannot be generated by the normal Alloc init method, and the derived subclass cannot produce the object, but only through the singleton method to get to the object

Source

Https://github.com/YouXianMing/SingletonPattern

// //   Singleton.h//  singletonpattern////  Created by Youxianming on 15/8/6. //   Copyright (c) 2015 youxianming. All rights reserved. // #import <Foundation/Foundation.h>@interface  singleton:nsobject+ (Singleton *) sharedinstance; @end
////singleton.m//Singletonpattern////Created by youxianming on 15/8/6.//Copyright (c) 2015 youxianming. All rights reserved.//#import "Singleton.h"#defineStr_singleton @ "Str_singleton"StaticSingleton *_sharedsingleton =Nil;@implementationSingleton+ (Singleton *) sharedinstance {Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{_sharedsingleton= (Singleton *) Str_singleton; _sharedsingleton=[[Singleton alloc] init];    }); return_sharedsingleton;}-(instancetype) init {nsstring*string= (NSString *) _sharedsingleton; if([stringIskindofclass:[nsstringclass]] && [stringIsequaltostring:str_singleton]) { Self=[Super Init]; if(self) {}returnSelf ; } Else {            returnNil; }}@end

Details

Tips to ensure that instances can only be obtained from the Shareinstance method

iOS design mode-single case

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.