Ios-3-objective-c Language: Single-case design mode

Source: Internet
Author: User

Summary: The singleton design pattern requires overriding the methods of the parent class. Because global instances do not allow deallocation, the Retain,release,autorelease methods associated with memory management need to be rewritten to avoid the memory aspects of the instance and to prevent the reference count from changing.

The specific instance code of the singleton pattern is as follows:

SingletonTeather.h file

#import <Foundation/Foundation.h>

@interface Singletonteather: NSObject

@property (Nonatomic,strong) NSString *name;

@property (nonatomic,assign) Nsinteger age;

@property (Nonatomic,strong) nsmutabledictionary *info;

+ (singletonteather *) Getsingletonteather;

@end

singletonteather.c file

#import "SingletonTeather.h"

Static Singletonteather *sharesingletonteather = Nil ;

@implementation Singletonteather

@synthesize Name,info;

+ (singletonteather *) Getsingletonteather

{

if (sharesingletonteather = =nil) {

Sharesingletonteather = [[Singletonteatheralloc]init];

}

return sharesingletonteather;

}

+ (ID) allocwithzone: ( struct _nszone *) Zone

{

if (sharesingletonteather = =nil) {

sharesingletonteather = [[SuperAllocwithzone:zone]init];

}

return sharesingletonteather;

}

-(ID) copywithzone: ( struct _nszone *) Zone

{

return self;

}

-(nsuinteger) Retaincount

{

return Nsintegermax;

}

-(ID) retain

{

return self;

}

-(onewayvoid) Release

{

}

-(ID) autorelease

{

return self;

}

@end

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

Ios-3-objective-c Language: Single-case design mode

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.