From someone else's written object-c. Some understandings in Singleton (singleton) pattern

Source: Internet
Author: User

about object-oriented design patterns

For the object-oriented design pattern, we must not be unfamiliar with it.

Throughout the 23 design patterns, the Singleton and the factory model (Factory method) are the most familiar and basic. Of course, this paper summarizes the singleton model, and does not narrate other design patterns.

Singleton mode, which is a singleton mode. As the name implies, is mainly used to do the application of resource sharing control. Many uses??

In essence, a singleton is a class that has and has been instantiated only once in the program declaration cycle. To ensure that the instantiation is unique, use the class's Class (static) method to generate and access the object.

At this point, you can access the class's singleton object anywhere in the program, because only the unique one is instantiated, so the Alloc, INIT, Autorelease initialization methods are not used.

Implementation of Singleton mode

In Objective-c, implementing the Singleton mode requires only the following four steps:

1. Implement a static instance for Singleton Object, initialize it, and set it to nil;

2. Implement an instance construction method (usually named Sharedinstance or Sharedmanager) to check if the static instance declared above is nil, and if so, create a new instance of this class;

3. Rewrite the Allocwithzone: method to ensure that when other people use alloc and Init to try to obtain a new instance, no new instance is generated.

4. Appropriate implementation of Allocwithzone, Copywithzone, Release and Autorelease.

Sinleton.h header File

[CPP]View Plaincopy
  1. //
  2. Singleton.h
  3. Singleton
  4. //
  5. Created by Leondun on 11-4-20.
  6. Copyright Leondun. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. @interface Singleton:nsobject {
  10. }
  11. + (Singleton *) Sharedinstancemethod;
  12. @end

SINGLETON.M implementation file

[CPP]View Plaincopy
  1. #import "Singleton.h"
  2. Static Singleton * sharedinstance = nil;
  3. @implementation Singleton
  4. Get a single case
  5. + (Singleton *) Sharedinstancemethod
  6. {
  7. @synchronized (self) {
  8. if (sharedinstance = = nil)
  9. Sharedinstance = [[Self alloc] init];
  10. }
  11. }
  12. return sharedinstance;
  13. }
  14. Only once alloc single case, then return nil
  15. + (ID) Allocwithzone: (Nszone *) zone
  16. {
  17. @synchronized (self) {
  18. if (sharedinstance = = nil) {
  19. instance = [Super Allocwithzone:zone];
  20. return instance;
  21. }
  22. }
  23. return nil;
  24. }
  25. Copy returns the single instance itself
  26. -(ID) Copywithzone: (Nszone *) zone
  27. {
  28. return self;
  29. }
  30. Retain returns the singleton itself
  31. -(ID) retain
  32. {
  33. return self;
  34. }
  35. The reference count is always 1
  36. -(unsigned) retaincount
  37. {
  38. return 1;
  39. }
  40. Release does not do any processing
  41. -(void) Release
  42. {
  43. }
  44. Autorelease returns the singleton itself
  45. -(ID) autorelease
  46. {
  47. return self;
  48. }
  49. //
  50. -(void) Dealloc
  51. {
  52. [Super Dealloc];
  53. }
  54. @end

Some code descriptions:

1. Synchronized this is primarily a multi-threaded program, which can limit the code within {} to one thread, and if a thread is not finished executing,

Other threads have to wait if they need to be executed.

2. Allocwithzone this is overloaded because this is an instance of reading information from the developed memory area, so if the required singleton already has,

It is necessary to prohibit modification of the current singleton, so return nil.

What is 3.allocWithZone, Copywithzone and Nszone?

Simply put, you can imagine a memory pool, alloc or dealloc these operations, and so on in this memory pool. Cocoa always assigns a default Nszone,

Any default memory operation is performed on this "Zone".

There is a flaw in using the default Nszone because it is global in scope and frequently used too much, which inevitably leads to fragmentation of memory. Especially when using a lot of alloc and Dealloc,

Performance will receive a certain impact. To do this, you can create a nszone yourself, and limit the alloc,copy to a "Zone".

Note: Thread safety issues are not considered here.

The 1.Singleton itself is thread safe.

The instance of 2.Singleton is thread safe.

Thread safety issues are not discussed here, donuts.

In fact, if you're sure you won't release the singleton prematurely, try using the following notation

static MyClass *instance = nil;

+ (ID) sharedinstance{

if (!instance) {

instance = [MyClass new];

}

return instance;

}

From someone else's written object-c. Some understandings in Singleton (singleton) pattern

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.