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
- //
- Singleton.h
- Singleton
- //
- Created by Leondun on 11-4-20.
- Copyright Leondun. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- @interface Singleton:nsobject {
- }
- + (Singleton *) Sharedinstancemethod;
- @end
SINGLETON.M implementation file
[CPP]View Plaincopy
- #import "Singleton.h"
- Static Singleton * sharedinstance = nil;
- @implementation Singleton
- Get a single case
- + (Singleton *) Sharedinstancemethod
- {
- @synchronized (self) {
- if (sharedinstance = = nil)
- Sharedinstance = [[Self alloc] init];
- }
- }
- return sharedinstance;
- }
- Only once alloc single case, then return nil
- + (ID) Allocwithzone: (Nszone *) zone
- {
- @synchronized (self) {
- if (sharedinstance = = nil) {
- instance = [Super Allocwithzone:zone];
- return instance;
- }
- }
- return nil;
- }
- Copy returns the single instance itself
- -(ID) Copywithzone: (Nszone *) zone
- {
- return self;
- }
- Retain returns the singleton itself
- -(ID) retain
- {
- return self;
- }
- The reference count is always 1
- -(unsigned) retaincount
- {
- return 1;
- }
- Release does not do any processing
- -(void) Release
- {
- }
- Autorelease returns the singleton itself
- -(ID) autorelease
- {
- return self;
- }
- //
- -(void) Dealloc
- {
- [Super Dealloc];
- }
- @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