Pergunta

O meu singleton acessor método é geralmente alguma variante:

static MyClass *gInstance = NULL;

+ (MyClass *)instance
{
    @synchronized(self)
    {
        if (gInstance == NULL)
            gInstance = [[self alloc] init];
    }

    return(gInstance);
}

O que poderia ser feito para melhorar isso?

Foi útil?

Solução

Outra opção é usar o método +(void)initialize. A partir da documentação:

O tempo de execução envia initialize a cada classe em um programa exatamente uma vez pouco antes da aula, ou qualquer classe que herda a partir dele, é enviado a sua primeira mensagem de dentro do programa. (Assim, o método nunca pode ser invocada se a classe não é usada.) O tempo de execução envia a mensagem initialize para aulas de uma forma thread-safe. Superclasses receber essa mensagem antes de suas subclasses.

Assim que você poderia fazer algo parecido com isto:

static MySingleton *sharedSingleton;

+ (void)initialize
{
    static BOOL initialized = NO;
    if(!initialized)
    {
        initialized = YES;
        sharedSingleton = [[MySingleton alloc] init];
    }
}

Outras dicas

@interface MySingleton : NSObject
{
}

+ (MySingleton *)sharedSingleton;
@end

@implementation MySingleton

+ (MySingleton *)sharedSingleton
{
  static MySingleton *sharedSingleton;

  @synchronized(self)
  {
    if (!sharedSingleton)
      sharedSingleton = [[MySingleton alloc] init];

    return sharedSingleton;
  }
}

@end

[Fonte]

Por minha outra resposta abaixo, eu acho que você deveria estar fazendo:

+ (id)sharedFoo
{
    static dispatch_once_t once;
    static MyFoo *sharedFoo;
    dispatch_once(&once, ^ { sharedFoo = [[self alloc] init]; });
    return sharedFoo;
}

Desde Kendall postou um threadsafe Singleton que tenta evitar custos de travamento, eu pensei que eu iria lançar um up, bem como:

#import <libkern/OSAtomic.h>

static void * volatile sharedInstance = nil;                                                

+ (className *) sharedInstance {                                                                    
  while (!sharedInstance) {                                                                          
    className *temp = [[self alloc] init];                                                                 
    if(!OSAtomicCompareAndSwapPtrBarrier(0x0, temp, &sharedInstance)) {
      [temp release];                                                                                   
    }                                                                                                    
  }                                                                                                        
  return sharedInstance;                                                                        
}

Ok, deixe-me explicar como isso funciona:

  1. caso rápido: Em sharedInstance execução normal já foi definido, então o loop while nunca é executado e a função retorna depois simplesmente testar para a existência da variável;

  2. caso lenta: Se sharedInstance não existe, em seguida, uma instância é alocado e copiado para ele usando um swap Comparar E ( 'CAS');

  3. caso sustentou: Se dois tópicos ambos tentam sharedInstance chamada ao mesmo tempo e sharedInstance não existe, ao mesmo tempo, em seguida, ambos irão inicializar novas instâncias do singleton e tentativa para CAS-lo em posição. Qualquer um que ganha o CAS retorna imediatamente, qualquer um perde libera a instância apenas alocados e retorna o (agora definida) sharedInstance. A única OSAtomicCompareAndSwapPtrBarrier atua como uma barreira de gravação para o segmento de configuração e uma barreira de leitura a partir do fio de teste.

static MyClass *sharedInst = nil;

+ (id)sharedInstance
{
    @synchronize( self ) {
        if ( sharedInst == nil ) {
            /* sharedInst set up in init */
            [[self alloc] init];
        }
    }
    return sharedInst;
}

- (id)init
{
    if ( sharedInst != nil ) {
        [NSException raise:NSInternalInconsistencyException
            format:@"[%@ %@] cannot be called; use +[%@ %@] instead"],
            NSStringFromClass([self class]), NSStringFromSelector(_cmd), 
            NSStringFromClass([self class]),
            NSStringFromSelector(@selector(sharedInstance)"];
    } else if ( self = [super init] ) {
        sharedInst = self;
        /* Whatever class specific here */
    }
    return sharedInst;
}

/* These probably do nothing in
   a GC app.  Keeps singleton
   as an actual singleton in a
   non CG app
*/
- (NSUInteger)retainCount
{
    return NSUIntegerMax;
}

- (oneway void)release
{
}

- (id)retain
{
    return sharedInst;
}

- (id)autorelease
{
    return sharedInst;
}

Editar: Esta implementação obsoleto com ARC. Por favor, dê uma olhada Como faço para implementar um singleton Objective-C que é compatível com ARC? para a implementação correta.

Todas as implementações de initialize que eu li em outras respostas compartilhar um erro comum.

+ (void) initialize {
  _instance = [[MySingletonClass alloc] init] // <----- Wrong!
}

+ (void) initialize {
  if (self == [MySingletonClass class]){ // <----- Correct!
      _instance = [[MySingletonClass alloc] init] 
  }
}

A documentação da Apple recomendo que você verifique o tipo de classe em seu bloco de inicialização. Porque subclasses chamar a inicialização por padrão. Existe um caso não-óbvia, onde subclasses podem ser criados indiretamente por meio de KVO. Porque, se você adicionar a seguinte linha em outra classe:

[[MySingletonClass getInstance] addObserver:self forKeyPath:@"foo" options:0 context:nil]

Objectivo-C irá criar implicitamente uma subclasse de MySingletonClass resultando em um segundo disparo de +initialize.

Você pode pensar que você deve verificar implicitamente para a inicialização duplicado em seu bloco de inicialização como tal:

- (id) init { <----- Wrong!
   if (_instance != nil) {
      // Some hack
   }
   else {
      // Do stuff
   }
  return self;
}

Mas você vai atirar no próprio pé; ou pior dar outro desenvolvedor a oportunidade de atirar no próprio pé.

- (id) init { <----- Correct!
   NSAssert(_instance == nil, @"Duplication initialization of singleton");
   self = [super init];
   if (self){
      // Do stuff
   }
   return self;
}

TL; DR, aqui está a minha aplicação

@implementation MySingletonClass
static MySingletonClass * _instance;
+ (void) initialize {
   if (self == [MySingletonClass class]){
      _instance = [[MySingletonClass alloc] init];
   }
}

- (id) init {
   ZAssert (_instance == nil, @"Duplication initialization of singleton");
   self = [super init];
   if (self) {
      // Initialization
   }
   return self;
}

+ (id) getInstance {
   return _instance;
}
@end

(Substituir ZAssert com a nossa própria afirmação macro;. Ou apenas NSAssert)

A explicação completa do código de macro Singleton é no blog Cacau With Love

http://cocoawithlove.com/2008/11 /singletons-appdelegates-and-top-level.html .

Eu tenho uma variação interessante sobre sharedInstance que é thread-safe, mas não bloqueia após a inicialização. Ainda não estou certo o suficiente para modificar a resposta superior, conforme solicitado, mas eu apresentá-lo para uma discussão mais aprofundada:

// Volatile to make sure we are not foiled by CPU caches
static volatile ALBackendRequestManager *sharedInstance;

// There's no need to call this directly, as method swizzling in sharedInstance
// means this will get called after the singleton is initialized.
+ (MySingleton *)simpleSharedInstance
{
    return (MySingleton *)sharedInstance;
}

+ (MySingleton*)sharedInstance
{
    @synchronized(self)
    {
        if (sharedInstance == nil)
        {
            sharedInstance = [[MySingleton alloc] init];
            // Replace expensive thread-safe method 
            // with the simpler one that just returns the allocated instance.
            SEL origSel = @selector(sharedInstance);
            SEL newSel = @selector(simpleSharedInstance);
            Method origMethod = class_getClassMethod(self, origSel);
            Method newMethod = class_getClassMethod(self, newSel);
            method_exchangeImplementations(origMethod, newMethod);
        }
    }
    return (MySingleton *)sharedInstance;
}

curta resposta:. Fabulous

Resposta longa: Algo como ....

static SomeSingleton *instance = NULL;

@implementation SomeSingleton

+ (id) instance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (instance == NULL){
            instance = [[super allocWithZone:NULL] init];
        }
    });
    return instance;
}

+ (id) allocWithZone:(NSZone *)paramZone {
    return [[self instance] retain];
}

- (id) copyWithZone:(NSZone *)paramZone {
    return self;
}

- (id) autorelease {
    return self;
}

- (NSUInteger) retainCount {
    return NSUIntegerMax;
}

- (id) retain {
    return self;
}

@end

Certifique-se de ler a expedição / once.h cabeçalho entender o que está acontecendo. Neste caso, os comentários de cabeçalho são mais aplicáveis ??do que a página docs ou homem.

Já rolou Singleton em uma classe, para que outras classes podem herdar Singleton propriedades.

Singleton.h:

static id sharedInstance = nil;

#define DEFINE_SHARED_INSTANCE + (id) sharedInstance {  return [self sharedInstance:&sharedInstance]; } \
                               + (id) allocWithZone:(NSZone *)zone { return [self allocWithZone:zone forInstance:&sharedInstance]; }

@interface Singleton : NSObject {

}

+ (id) sharedInstance;
+ (id) sharedInstance:(id*)inst;

+ (id) allocWithZone:(NSZone *)zone forInstance:(id*)inst;

@end

Singleton.m:

#import "Singleton.h"


@implementation Singleton


+ (id) sharedInstance { 
    return [self sharedInstance:&sharedInstance];
}

+ (id) sharedInstance:(id*)inst {
    @synchronized(self)
    {
        if (*inst == nil)
            *inst = [[self alloc] init];
    }
    return *inst;
}

+ (id) allocWithZone:(NSZone *)zone forInstance:(id*)inst {
    @synchronized(self) {
        if (*inst == nil) {
            *inst = [super allocWithZone:zone];
            return *inst;  // assignment and return on first allocation
        }
    }
    return nil; // on subsequent allocation attempts return nil
}

- (id)copyWithZone:(NSZone *)zone {
    return self;
}

- (id)retain {
    return self;
}

- (unsigned)retainCount {
    return UINT_MAX;  // denotes an object that cannot be released
}

- (void)release {
    //do nothing
}

- (id)autorelease {
    return self;
}


@end

E aqui está um exemplo de uma classe, que pretende se tornar Singleton.

#import "Singleton.h"

@interface SomeClass : Singleton {

}

@end

@implementation SomeClass 

DEFINE_SHARED_INSTANCE;

@end

A única limitação sobre a classe Singleton, é que é NSObject subclasse. Mas a maior parte do tempo eu uso singletons no meu código eles são de fato subclasses NSObject, então essa classe realmente aliviar a minha vida e código make limpo.

Isso funciona em um ambiente não-lixo coletado também.

@interface MySingleton : NSObject {
}

+(MySingleton *)sharedManager;

@end


@implementation MySingleton

static MySingleton *sharedMySingleton = nil;

+(MySingleton*)sharedManager {
    @synchronized(self) {
        if (sharedMySingleton == nil) {
            [[self alloc] init]; // assignment not done here
        }
    }
    return sharedMySingleton;
}


+(id)allocWithZone:(NSZone *)zone {
    @synchronized(self) {
        if (sharedMySingleton == nil) {
            sharedMySingleton = [super allocWithZone:zone];
            return sharedMySingleton;  // assignment and return on first allocation
        }
    }
    return nil; //on subsequent allocation attempts return nil
}


-(void)dealloc {
    [super dealloc];
}

-(id)copyWithZone:(NSZone *)zone {
    return self;
}


-(id)retain {
    return self;
}


-(unsigned)retainCount {
    return UINT_MAX;  //denotes an object that cannot be release
}


-(void)release {
    //do nothing    
}


-(id)autorelease {
    return self;    
}


-(id)init {
    self = [super init];
    sharedMySingleton = self;

    //initialize here

    return self;
}

@end

não deve este ser thread seguro e evitar o bloqueio dispendioso após a primeira chamada?

+ (MySingleton*)sharedInstance
{
    if (sharedInstance == nil) {
        @synchronized(self) {
            if (sharedInstance == nil) {
                sharedInstance = [[MySingleton alloc] init];
            }
        }
    }
    return (MySingleton *)sharedInstance;
}

Como cerca

static MyClass *gInstance = NULL;

+ (MyClass *)instance
{
    if (gInstance == NULL) {
        @synchronized(self)
        {
            if (gInstance == NULL)
                gInstance = [[self alloc] init];
        }
    }

    return(gInstance);
}

Assim, você evita o custo de sincronização após a inicialização?

Para uma discussão aprofundada do padrão singleton em Objective-C, veja aqui:

Usando o padrão Singleton em Objective-C

KLSingleton é:

  1. Subclassible (para o grau de ordem n)
  2. ARC compatíveis
  3. Seguro com alloc e init
  4. Loaded preguiçosamente
  5. thread-safe
  6. Lock-livre (usos + initialize, não @synchronize)
  7. Macro-livre
  8. Swizzle-livre
  9. Simples

KLSingleton

Você não deseja sincronizar na auto ... Desde o objeto auto ainda não existe! Você acaba bloqueio em um valor id temporário. Você quer garantir que ninguém mais pode executar métodos de classe (sharedInstance, alloc, allocWithZone :, etc), então você precisa para sincronizar sobre o objeto de classe em vez disso:

@implementation MYSingleton

static MYSingleton * sharedInstance = nil;

+( id )sharedInstance {
    @synchronized( [ MYSingleton class ] ) {
        if( sharedInstance == nil )
            sharedInstance = [ [ MYSingleton alloc ] init ];
    }

    return sharedInstance;
}

+( id )allocWithZone:( NSZone * )zone {
    @synchronized( [ MYSingleton class ] ) {
        if( sharedInstance == nil )
            sharedInstance = [ super allocWithZone:zone ];
    }

    return sharedInstance;
}

-( id )init {
    @synchronized( [ MYSingleton class ] ) {
        self = [ super init ];
        if( self != nil ) {
            // Insert initialization code here
        }

        return self;
    }
}

@end

Apenas queria deixar isso aqui para que eu não perdê-lo. A vantagem a esta é que é utilizável em InterfaceBuilder, o que é uma vantagem enorme. Isto é tomado de uma outra pergunta que eu pedi :

static Server *instance;

+ (Server *)instance { return instance; }

+ (id)hiddenAlloc
{
    return [super alloc];
}

+ (id)alloc
{
    return [[self instance] retain];
}


+ (void)initialize
{
    static BOOL initialized = NO;
    if(!initialized)
    {
        initialized = YES;
        instance = [[Server hiddenAlloc] init];
    }
}

- (id) init
{
    if (instance)
        return self;
    self = [super init];
    if (self != nil) {
        // whatever
    }
    return self;
}
static mySingleton *obj=nil;

@implementation mySingleton

-(id) init {
    if(obj != nil){     
        [self release];
        return obj;
    } else if(self = [super init]) {
        obj = self;
    }   
    return obj;
}

+(mySingleton*) getSharedInstance {
    @synchronized(self){
        if(obj == nil) {
            obj = [[mySingleton alloc] init];
        }
    }
    return obj;
}

- (id)retain {
    return self;
}

- (id)copy {
    return self;
}

- (unsigned)retainCount {
    return UINT_MAX;  // denotes an object that cannot be released
}

- (void)release {
    if(obj != self){
        [super release];
    }
    //do nothing
}

- (id)autorelease {
    return self;
}

-(void) dealloc {
    [super dealloc];
}
@end

Eu sei que há um monte de comentários sobre esta "questão", mas eu não vejo muitas pessoas sugerem usando uma macro para definir o singleton. É um padrão tão comum e uma macro simplifica muito o singleton.

Aqui estão as macros que eu escrevi com base em várias implementações ObjC que eu vi.

Singeton.h

/**
 @abstract  Helps define the interface of a singleton.
 @param  TYPE  The type of this singleton.
 @param  NAME  The name of the singleton accessor.  Must match the name used in the implementation.
 @discussion
 Typcially the NAME is something like 'sharedThing' where 'Thing' is the prefix-removed type name of the class.
 */
#define SingletonInterface(TYPE, NAME) \
+ (TYPE *)NAME;


/**
 @abstract  Helps define the implementation of a singleton.
 @param  TYPE  The type of this singleton.
 @param  NAME  The name of the singleton accessor.  Must match the name used in the interface.
 @discussion
 Typcially the NAME is something like 'sharedThing' where 'Thing' is the prefix-removed type name of the class.
 */
#define SingletonImplementation(TYPE, NAME) \
static TYPE *__ ## NAME; \
\
\
+ (void)initialize \
{ \
    static BOOL initialized = NO; \
    if(!initialized) \
    { \
        initialized = YES; \
        __ ## NAME = [[TYPE alloc] init]; \
    } \
} \
\
\
+ (TYPE *)NAME \
{ \
    return __ ## NAME; \
}

Exemplo de utilização:

MyManager.h

@interface MyManager

SingletonInterface(MyManager, sharedManager);

// ...

@end

MyManager.m

@implementation MyManager

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

SingletonImplementation(MyManager, sharedManager);

// ...

@end

Por que um macro interface quando ele está quase vazio? consistência código entre os arquivos de cabeçalho e código; manutenção no caso de você querer adicionar mais métodos automáticos ou alterá-lo ao redor.

Eu estou usando o método de inicialização para criar o singleton como é usado na resposta mais popular aqui (no momento da escrita).

Com os métodos da classe C Objectivo, podemos simplesmente evitar o uso do padrão singleton da maneira usual, a partir de:

[[Librarian sharedInstance] openLibrary]

para:

[Librarian openLibrary]

envolvendo a classe dentro de outra classe que só tem Métodos de Classe , dessa forma não há nenhuma chance de criar acidentalmente instâncias duplicadas, como nós não estamos criando qualquer instância!

Eu escrevi um blog mais detalhada aqui :)

Para estender o exemplo de @ Robbie-Hanson ...

static MySingleton* sharedSingleton = nil;

+ (void)initialize {
    static BOOL initialized = NO;
    if (!initialized) {
        initialized = YES;
        sharedSingleton = [[self alloc] init];
    }
}

- (id)init {
    self = [super init];
    if (self) {
        // Member initialization here.
    }
    return self;
}

O meu caminho é simples assim:

static id instanceOfXXX = nil;

+ (id) sharedXXX
{
    static volatile BOOL initialized = NO;

    if (!initialized)
    {
        @synchronized([XXX class])
        {
            if (!initialized)
            {
                instanceOfXXX = [[XXX alloc] init];
                initialized = YES;
            }
        }
    }

    return instanceOfXXX;
}

Se o singleton já está inicializado, o bloco BLOQUEIO não serão inseridos. A segunda verificação if (! Inicializado) é certificar-se que não é inicializado no entanto, quando o segmento atual adquire o bloqueio.

Eu não li através de todas as soluções, de modo perdoar se este código é redundante.

Este é o mais implementação segmento seguro na minha opinião.

+(SingletonObject *) sharedManager
{
    static SingletonObject * sharedResourcesObj = nil;

    @synchronized(self)
    {
        if (!sharedResourcesObj)
        {
            sharedResourcesObj = [[SingletonObject alloc] init];
        }
    }

    return sharedResourcesObj;
}

Eu costumo usar o código mais ou menos semelhante à resposta de Ben Hoffstein (que eu também saí da Wikipedia). Eu usá-lo pelas razões expostas por Chris Hanson em seu comentário.

No entanto, às vezes eu tenho uma necessidade de colocar um singleton em um NIB, e, nesse caso, eu uso o seguinte:

@implementation Singleton

static Singleton *singleton = nil;

- (id)init {
    static BOOL initialized = NO;
    if (!initialized) {
        self = [super init];
        singleton = self;
        initialized = YES;
    }
    return self;
}

+ (id)allocWithZone:(NSZone*)zone {
    @synchronized (self) {
        if (!singleton)
            singleton = [super allocWithZone:zone];     
    }
    return singleton;
}

+ (Singleton*)sharedSingleton {
    if (!singleton)
        [[Singleton alloc] init];
    return singleton;
}

@end

Deixo a implementação de -retain (etc.) para o leitor, embora o código acima é tudo que você precisa em um ambiente de lixo coletado.

A resposta aceita, embora ele compila, está incorreto.

+ (MySingleton*)sharedInstance
{
    @synchronized(self)  <-------- self does not exist at class scope
    {
        if (sharedInstance == nil)
            sharedInstance = [[MySingleton alloc] init];
    }
    return sharedInstance;
}

documentação Per Apple:

... Você pode ter uma abordagem semelhante para sincronizar os métodos de classe da classe associada, usando o objeto de classe em vez de si mesmo.

Mesmo usando auto obras, não deve e esta parece ser uma cópia e cole erro para mim. A implementação correta para um método de classe fábrica seria:

+ (MySingleton*)getInstance
{
    @synchronized([MySingleton class]) 
    {
        if (sharedInstance == nil)
            sharedInstance = [[MySingleton alloc] init];
    }
    return sharedInstance;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top