iOS Development Basics Object-c (12)-Singleton mode

Source: Internet
Author: User

The singleton pattern means that there is only one instance.

The singleton pattern ensures that a class has only one instance, and instantiates itself and provides this instance to the system as a whole. This class is called a singleton class.

1. Key points of the singleton mode:

  Obviously, there are three main points of the singleton pattern, one is that a class can have only one instance, and the other is that it has to create this instance on its own, and thirdly, it must provide this instance to the whole system on its own.

2. Advantages of Singleton mode:

1. Instance control: Singleton prevents other objects from instantiating copies of their own Singleton objects, ensuring that all objects have access to unique instances.

2. Flexibility: Because classes control the instantiation process, classes can more flexibly alter the instantiation process

Let's take a sample.

Say. We need to have a bank that can save and withdraw money from the bank. The total amount of money in a bank will change as it is saved and taken.


Let's create a new class of banking class

  bank.h//  08-bank////  Created by Cuixuerui on 15/12/29.//  Copyright (c) 2015 Cuixuerui. All rights reserved.//#import <Foundation/Foundation.h> @interface bank:nsobject@property int money;-( Instancetype) Initwithmoney: (int) money;+ (Bank *) Defaualtbank; @end

Implementation algorithm BANK.M

  bank.m//  08-bank////  Created by Cuixuerui on 15/12/29.//  Copyright (c) 2015 Cuixuerui. All rights reserved.//#import "Bank.h" @implementation bank-(Instancetype) Initwithmoney: (int.) Money {        if (self =[ Super Init]) {         _money=money;    }    return self;} + (Bank *) Defaualtbank {        Static bank *bank;//global variable    if (!bank) {//This sentence ensures that there is only one instance of        bank = [[Bank alloc] initwithmoney:1000];//create an instance by itself    }    return bank;    @end

In fact, the most important thing is
+ (Bank *) Defaualtbank;
This is the key to the singleton pattern.

Let's create a new person class here.

  personone.h//  08-bank////  Created by Cuixuerui on 15/12/29.//  Copyright (c) 2015 Cuixuerui. All rights reserved.//#import <Foundation/Foundation.h> #import "Bank.h" @interface personone:nsobject{        Bank * _bank;} -(void) Withdrawmoney: (int) money;-(void) Savemoney: (int) money;//-(instancetype) init; @end

Implementing the Person Class


  personone.m//  08-bank////  Created by Cuixuerui on 15/12/29.//  Copyright (c) 2015 Cuixuerui. All rights reserved.//#import "PersonOne.h" #import "Bank.h" @implementation personone-(instancetype) init{        if ( self = [Super init]) {        _bank = [Bank Defaualtbank];    }    return self;} -(void) Savemoney: (int) Money {        //must be created using the method of creating a singleton object to create        [_bank setmoney:[_bank Money]+money];            NSLog (@ "The Bank also has%d", [_bank money]);        } -(void) Withdrawmoney: (int) Money {            [_bank setmoney:[_bank Money]-money];        NSLog (@ "The Bank also has%d", [_bank money]);    } @end

The last call in the Main method


  main.m//  08-bank////  Created by Cuixuerui on 15/12/29.//  Copyright (c) 2015 Cuixuerui. All rights reserved.//#import <Foundation/Foundation.h> #import "PersonOne.h" int main (int argc, const char * argv[] {       Personone *p1 = [[Personone alloc] init];            [P1 savemoney:500];            return 0;}



iOS Development Basics Object-c (12)-Singleton mode

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.