A single example design pattern for iOS development design patterns

Source: Internet
Author: User
Tags naming convention

    • Single Case design mode

is a common software design pattern that ensures that a class in the system has only one instance and that the instance is easily accessible by the outside world.

In English: Singleton, the Meaning of mathematics is: there is only one set of elements.


Singleton mode is the best solution if you want an object of a class in your system to exist only one

Advantages:

You can prevent other objects from instantiating a copy of an object, ensuring that all objects have access to a unique instance

Disadvantages:

Once a singleton object is created, the object pointer is saved in the global static zone, and the singleton object allocates memory space in the heap and is not released until the application terminates


Classes that use the Singleton design pattern: (Most of the resources used to manage the system)

UIApplication---represents the current app

Uifilemanager---for managing folder and file properties

Uidevice---Describe current device information, such as size, system version, etc.

...


    • Simple implementation of single case

If you want a class to use a singleton design pattern, complete the implementation in the following steps, where the first three steps are required

1) in the source file of the class, add a static object pointer to record the address of the Singleton object. Static guarantees that they cannot be accessed directly from other source files.

such as: Player class Amplayertool

Static Amplayertool * tool;

2) provides a class method that returns the address of a singleton object and uses the method of deferred creation

The naming convention for this class of methods: Sharedxxx, such as:

+ (Instancetype) sharedplayertool{if (tool = = nil) {tool = [[Amplayertool allocwithzone:nil] init]; } return tool;

3) guarantee that you cannot create an object with a single exception by using the Alloc method

The Alloc method calls Allocwithzone:, which should be overridden, such as:

+ (Instancetype) Allocwithzone: (struct _nszone *) zone{if (tool = nil) {tool = [super Allocwithzone:zone]; } return tool;

This ensures that no matter alloc or Allocwithzone: The number of times the call returns the address of the same object.

4) If you consider the copy behavior of the object, you should also override the Copywithzone: method

Overrides should ensure that no new objects are created, such as:

-(ID) Copywithzone: (struct _nszone *) zone {return self}

5) If memory management uses non-arc, also consider overriding the Retain method

It is necessary to ensure that the single object release is destroyed once, and retain does not make any sense, such as:

-(ID) retain{return self;}


    • Implementation of single-instance design pattern in multi-threaded environment

Dispatch_once is thread-safe and can be implemented in a multi-threaded environment where the code is executed only once.

Modify the following steps in the simple implementation

2) provides a class method that returns the address of a singleton object

+ (instancetype) sharedplayertool{static dispatch_once_t Oncetoken;    Dispatch_once (&oncetoken, ^{tool = [[Amplayertool allocwithzone:nil] init];    }); return tool;}

3) guarantee that you cannot create an object with a single exception by using the Alloc method

+ (Instancetype) Allocwithzone: (struct _nszone *) zone{static dispatch_once_t Oncetoken;    Dispatch_once (&oncetoken, ^{tool = [[Amplayertool allocwithzone:nil] init];    }); return tool;}






This article is from the "Teacheran" blog, make sure to keep this source http://annmeng.blog.51cto.com/3321237/1746593

A single example design pattern for iOS development design patterns

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.