Single-Case mode

Source: Internet
Author: User
Tags vars

Singleton mode is one of the common design patterns in iOS. The role of a singleton design pattern is to make an object of this class the only instance in the system, so it is necessary to create the object in a unique way and return the address of the object. So, when do we use singleton mode? 1. A class can have only one instance, and it must be accessed from a well-known access point. 2. This unique instance can only be extended by subclasses, and the extended object does not break the client code.

So how do you implement a singleton pattern with objective-c? Let's create a new singleton class, implemented in Singleton.h as follows

    1. @interface Singleton:nsobject
    2. + (Singleton *) sharedinstance;
    3. @end

In SINGLETON.M

  1. @implementation Singleton
  2. Static Singleton * Sharedsingleton = nil;
  3. + (Singleton *) sharedinstance
  4. {
  5. if (Sharedsingleton = = nil) {
  6. Sharedsingleton = [[Singleton alloc] init];
  7. }
  8. return Sharedsingleton;
  9. }
  10. @end


This creates a simple singleton pattern that is actually implemented by some programmers, but in fact it is not a "strict" version, and in practice, it is possible to encounter an object that initiates a call that cannot instantiate a singleton object in another way, or create multiple instances. (Someone has discussed this problem with me before, saying that the user should strictly follow the interface to use, when actually singleton is an object, we can not guarantee that the user will not use other methods to create (such as Alloc), this time he will create multiple instances, This will cause these imperceptible bugs to occur)

Now I'm making improvements to SINGLETON.M.

  1. @implementation Singleton
  2. Static Singleton * Sharedsingleton = nil;
  3. + (Singleton *) sharedinstance
  4. {
  5. if (Sharedsingleton = = nil) {
  6. Sharedsingleton = [[super Allocwithzone:NULL] init];
  7. }
  8. return Sharedsingleton;
  9. }
  10. + (ID) allocwithzone: (struct _nszone *) zone
  11. {
  12. return [[self sharedinstance] retain];
  13. }
  14. -(ID) copywithzone: (nszone *) zone
  15. {
  16. return self ;
  17. }
  18. -(ID) retain
  19. {
  20. return self ;
  21. }
  22. -(Nsuinteger) Retaincount
  23. {
  24. return Nsuintegermax;
  25. }
  26. -(void) Release
  27. {
  28. //  
  29. }
  30. -(ID) autorelease
  31. {
  32. return self ;
  33. }
  34. @end


Maybe you noticed, I overloaded the Allocwithzone: The Singleton object returned from the Sharedinstance method is maintained, and the user returns a unique instance even when using Alloc (the Alloc method calls Allocwithzone: Create object first). and retain and other memory management functions are overloaded, so that we have the Singleton class to become "strict".

Single-Case 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.