2013-04-15 18 views
3

Nomaly我用來建立一個單身以下幾招:在Objective-c中的單例類上設置init可配置變量的好方法是什麼?

+ (MyClass *)sharedInstance 
{  
    static MyClass *_sharedInstance = nil; 
    static dispatch_once_t oncePredicate; 

    dispatch_once(&oncePredicate, ^{ 
     _sharedInstance = [[self alloc] init]; 
     // Init variables 
    }); 

    return _sharedInstance; 
} 

然後我可以調用方法如下:

[[MyClass sharedInstance] anyInstanceMethod]; 

但是,發生什麼事時,任何初始化變量是從類的外部配置屬性? 我的方法是創建兩個類方法,用配置屬性變量其中之一:

+ (MyClass *)sharedInstanceWithVariableOne:(NSString*)aParamOne andVariableTwo:(NSString*)aParamTwo 
{  
    static MyClass *_sharedInstance = nil; 
    static dispatch_once_t oncePredicate; 

    dispatch_once(&oncePredicate, ^{ 
     _sharedInstance = [[self alloc] init]; 

     // Init configurables variables 
     _sharedInstance.paramOne = aParamOne; 
     _sharedInstance.paramTwo = aParamTwo; 
    }); 

    return _sharedInstance; 
} 

,第二個作爲代理這最後一個使用默認值:

+ (MyClass *)sharedInstance 
    {  
     return [MyClass sharedInstanceWithVariableOne:@"value1" andVariableTwo:@"value2"]; 
    } 

所以,如果你想使用帶配置變量的單例類,您應該首先調用sharedInstanceWithVariableOne:andVariableTwo,然後再調用sharedInstance。 我認爲這種方法不是最好的,我期待着使用他人。

在此先感謝。

+0

爲什麼你不喜歡嗎? –

+5

我覺得這種模式有點可疑。考慮多次調用'sharedInstanceWithVariableOne:...'會發生什麼。只有第一組參數會產生任何影響,其他參數將獲得相同的共享實例,但不會與預期的參數一起使用。每個配置都應該是一個單獨的對象,或者這些參數應該是共享實例的屬性,只要您需要更改它們就可以設置這些屬性。 – omz

+0

Anoop Vaidya,我認爲這可能會有更好的方法。這可能會讓人感到困惑和不明顯。 – martinezdelariva

回答

3

感謝您的快速響應。如你所說,這不是一個單身人士。 我將使用與[NSURLCache setSharedURLCache:][NSURLCache sharedURLCache]相同的行爲。

接口

// MyClass.h 
@interface MyClass : NSObject 

+ (MyClass *)sharedMyClass; 
+ (void)setSharedMyClass:(MyClass *)object; 

- (id)initWithParamOne:(NSString)p1 andParamTwo:(NSString)p2; 
@end 

實施

// MyClass.m 
@interface MyClass() 
@property(nonatomic, strong) NSString *paramOne; 
@property(nonatomic, strong) NSString *paramTwo; 
@end; 

@implementation MyClass 

static MyClass *_sharedInstance = nil; 

+ (MyClass *)sharedMyClass 
{ 
    if (_sharedInstance == nil) { 
     _sharedInstance = [[MyClass alloc] initWithParamOne:@"value1" andParamTwo:@"value2"]; 
    } 

    return _sharedInstance ; 
} 

+ (void)setSharedMyClass:(MyClass *)object 
{ 
    _sharedInstance = object; 
} 

- (id)initWithParamOne:(NSString)p1 andParamTwo:(NSString)p2 
{ 
    self = [super init]; 
    if (self) { 
     _paramOne = p1; 
     _paramTwo = p2; 
    } 

    return self; 
} 

然後,我可以用[[MyClass sharedMyClass] anyMethod]

+0

add dispatch_once爲什麼刪除它? – hariszaman

1

有很多方法可以設計您的共享實例。 (這不是一個singleton儘可能多的評論指出,因爲只能有一個這個類的實例可能。但是那個共享實例模式通常被用作沒有直接調用的「窮人」單身人士。任何init方法回到問題...)也許下面會適合你:

+ (void) setSharedInstanceParameterOne:(NSString*)aParamOne 
         andParameterTwo:(NSString*)aParamTwo 
{  
    MyClass *_sharedInstance = self.sharedInstance; 
    _sharedInstance.paramOne = aParamOne; 
    _sharedInstance.paramTwo = aParamTwo; 
} 

現在你用[MyClass setSharedInstanceParameterOne:p1 andParameterTwo:p2]設置/更改共享實例的參數,和[MyClass sharedInstance](或MyClass.sharedInstance,雖然有些可能會對此進行哲學討論)來訪問它。

+0

你能否詳細說明這種模式?我無法讓它工作。 – kursus

+0

@kursus - 什麼不適合你?您有兩種方法,一種是獲取共享實例(例如OP的第一種方法),另一種是方便更改其配置的方法(上述方法)。 – CRD

+0

超級棒! –

相關問題