<span id="Label3"></p><p><p><strong>Design</strong> <strong>mode:</strong> <strong>single</strong> <strong>Example</strong></p></p><p><p>I. Singleton mode is a common software design Pattern. In its core structure, it contains only a special class called a singleton class. The singleton mode can ensure that there is only one instance of a class in the system, and the instance is easy to be accessed by the outside world, thus it is convenient to control the number of instances and save system Resources. Singleton mode is the best solution if you want to have only one object for a class in the System.</p></p><p><p>The main points of the singleton pattern are three: one is that a class can have only one instance, and the other is that it must create the instance itself, and thirdly, it must provide this instance to the system itself.</p></p><p align="left"><p align="left">Two. single-case Mode Structure diagram:</p></p><p align="left"><p align="left"></p></p><p align="left"><p align="left">Three. benefits of a single case:</p></p><p><p>1. Instance control: Singleton mode prevents other objects from instantiating copies of their own singleton objects, ensuring that all objects have access to unique Instances.</p></p><p><p>2. Flexibility: because classes control the instantiation process, classes can flexibly change the instantiation process</p></p><p><p>Four. Single Sample Demo</p></p><p><p>Creating a single case</p></p><p><p>. h file</p></p><p><p>#import <Foundation/Foundation.h></p></p><p><p></p></p><p><p>@interface oneexample:nsobject<nscopying></p></p><p><p></p></p><p><p>+ (id) oneexample;</p></p><p><p></p></p><p><p>@end</p></p><p><p>. m file</p></p><p><p>First create a static class object and set him to Empty.</p></p><p><p>Static Oneexample * Oneexample = nil;</p></p><p><p>+ (id) Oneexample</p></p><p><p>{</p></p><p><p>Double locking mechanism</p></p><p><p>if (oneexample = = nil)//avoid multiple locks</p></p><p><p>{</p></p><p><p>@synchronized (self)//allow only one entry at a time</p></p><p><p>{</p></p><p><p>if (oneexample = = Nil)</p></p><p><p>{</p></p><p><p>Oneexample = [[self class] alloc] init];</p></p><p><p>}</p></p><p><p>}</p></p><p><p>}</p></p><p><p>Return oneexample;</p></p><p><p>}</p></p><p><p>Generally speaking, We've created the singleton now.</p></p><p><p>But because the singleton is unique, we know its usage when we create our own singleton, but other people don't know when we create a singleton, but the method, which uses the new Alloc copy, creates an object, so that the singleton we create is meaningless. In order to avoid this, we need to reset his other methods to create this Object.</p></p><p><p>To set the new method</p></p><p><p>+ (id) New</p></p><p><p>{</p></p><p><p>return [self alloc];</p></p><p><p>}</p></p><p><p>Ways to set up Alloc</p></p><p><p>+ (id) allocwithzone: (nszone *) Zone</p></p><p><p>{</p></p><p><p>if (oneexample = = Nil)</p></p><p><p>{</p></p><p><p>@synchronized (self)</p></p><p><p>{</p></p><p><p>if (oneexample = = Nil)</p></p><p><p>{</p></p><p><p>Oneexample = [super allocwithzone:zone];</p></p><p><p>}</p></p><p><p>}</p></p><p><p>}</p></p><p><p>Return nil;</p></p><p><p>}</p></p><p><p>How to set up copy</p></p><p><p>-(id) copywithzone: (nszone *) Zone</p></p><p><p>{</p></p><p><p>Return oneexample;</p></p><p><p>}</p></p><p><p></p></p><p><p>-(id) mutablecopywithzone: (nszone *) Zone</p></p><p><p>{</p></p><p><p>return [self copywithzone:zone];</p></p><p><p>}</p></p><p><p>After we reset these kinds of methods to create objects, we can guarantee that this object is a singleton, regardless of how the object is worn, and the singleton we encapsulate is perfect.</p></p><p><p></p></p><p><p>A single example of IOS design patterns</p></p></span>
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