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.
...
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;}
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